Static Toast routine for debugging

Viewed 147

I'm trying to debug some code on an obscure Android device that does not have an ADB (Android debug bridge) driver for Android Studio so I cannot step through code on the device with the Android Studio debugger so I'm debugging blindly.

So I thought I would use an old-fashioned "printf" strategy by inserting little Android toasts with various debug information in my code where I needed them. Some of the code I'm calling this from are static and some aren't.

I made a "make toast" routine

// make a Toast I can use for debugging
private static void makeToast(String msg) {
    Toast toast = Toast.makeText(thisActivity, msg, Toast.LENGTH_SHORT);
    toast.setGravity(Gravity.CENTER, 0, 200);  // x offset , y offset
    toast.show();
}

The "this" activity is declared thusly:

 private static PlyListActivity thisActivity = null;

And initialised in OnCreate ...

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //We need another instance to avoid a static reference
        selBox = new SelBoxListener();  //was final SelBoxListener 
        RemoteControlActivity.curContext = this;
        thisActivity = this;
 . . . 

Here's an example of it being called . . .

public static void updateBillboard(final int resColor, final String imsg2) {
    //Are we created yet?
    if (plyBillboard != null) {
        plyBillboard.post(new Runnable() {
            public void run() {
                plyBillboard.setTextColor(resColor);
                plyBillboard.setText(imsg2);
            }
        });
    } //fi
   //!! thisActivity.finish();
    makeToast("in UpdateBillboard");
} 

No toast pops up. If I un-comment out the call to finish() it exits the Activity, thus confirming I am executing the code. . .

So what am I doing wrong and how do I get my toasts to appear?

Edit: one responder suggested I needed to put the call to makeToast() inside the runnable so it would be called. But that's why I noted that if I uncomment the call to finish() it causes to Activity to exit. Indeed, if I place a call to finish() inside makeToast() it does the same thing, so the call to makeToast() IS being executed.

2 Answers

When you look at the definition of Runnable " When an object implementing interface Runnable is used to create a thread, starting the thread causes the object's run method to be called in that separately executing thread."

new Handler(Looper.getMainLooper()).post(new Runnable() {
     @Override
     public void run() {
             Log.d("Thread", "I am on UI thread");
          showToastHere();
      } 
    });

My guess is that the method updateBillboard is not running inside the UI Thread, so one way to make it work is to use a Handler. For example:

private static Handler mHandler;
@Override
onCreate(Bundle...) {
    super.onCreate(...);
    mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message inputMessage) {
            String message = (String) inputMessage.obj;
            makeToast(message);
        }
    }
}   

updateBillboard(...) {
    ...
    Message message = mHandler.obtainMessage(0, "Hello");
    message.sendToTarget();
}
Related