-2

I know this is a old question with many answers, like what I found here. But I still cannot see my toast message.

I tried check the current thread like:

Log.d("DEBUG", "Main Looper: " + Looper.getMainLooper().toString() + ", Current Looper: " + Looper.myLooper().toString());
Log.d("DEBUG", "Main Thread: " + Looper.getMainLooper().getThread().toString() + ", Current Thread: " + Thread.currentThread().toString());

//and the log is like:
D/DEBUG﹕ Main Looper: Looper{420a9e68}, Current Looper: Looper{420a9e68}
D/DEBUG﹕ Main Thread: Thread[main,5,main], Current Thread: Thread[main,5,main]

so I think I'm in the main thread(ui thread), and I also tried toast in ui thread specifically:

runOnUiThread(new Runnable() {
    @Override
    public void run() {
        Toast.makeText(getBaseContext(), "abc", Toast.LENGTH_LONG).show();
        Log.d("DEBUG", "ui thread abc");
    }
});

I can see the log "ui thread abc" but the toast still didn't appeared.

Then I think maybe the toast is overlapped by other views so I tried to specify the location of toast like: Toast toast Toast.makeText(getBaseContext(), "abc", Toast.LENGTH_LONG);

Toast toast = Toast.makeText(this, "abc", Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP|Gravity.CENTER, 0, 0);
toast.show();

Still get nothing. So what do you think is happening to my toast?

-------UPDATE

I'm in a activity with a vertical LinearLayout and several embedded horizontal LinearLayouts. I got only two activities now and I'm toasting in the main acvitity.

I'm using a custom image slider and toasting in the onclick event, the onClickMethod is like below:

public void onSliderClick(BaseSliderView slider) {
//        Log.d("DEBUG", "Main Looper: " + Looper.getMainLooper().toString() + ", Current Looper: " + Looper.myLooper().toString());
//        Log.d("DEBUG", "Main Thread: " + Looper.getMainLooper().getThread().toString() + ", Current Thread: " + Thread.currentThread().toString());
//        Toast.makeText(getBaseContext(), slider.getBundle().get("testMsg").toString(), Toast.LENGTH_LONG).show();

//        Toast.makeText(getApplicationContext(), slider.getBundle().get("testMsg").toString(), Toast.LENGTH_LONG).show();
//        Log.d("DEBUG", "Slider - " + slider.getBundle().get("testMsg").toString());

    Toast.makeText(getApplicationContext(), "abc", Toast.LENGTH_LONG).show();
    Log.d("DEBUG", "msg abc 123");


//        Toast toast = Toast.makeText(this, "abc", Toast.LENGTH_SHORT);
//        toast.setGravity(Gravity.TOP|Gravity.CENTER, 0, 0);
//        toast.show();
}

-------UPDATE 2

I just found that the action bar toast is not showing too, I did set the title element, I'm using a split action bar which is placed at the bottom of the screen.

Actually I can see all the toast messages before, then I made some change to the UI or some other stuffs, then all the toast disappeared. The problem is that I can't rollback to the older version which the toast is working good and I can't find the reason why they are gone now.

By the way after I added the custom image slider the toast is still good, and there isn't any other threads except the background thread which is auto switching the image slider.

Community
  • 1
  • 1
MagicFingr
  • 479
  • 6
  • 20

3 Answers3

1

i faced same problem a month ago when i was trying to make toast from doInBackground(String... params) method of Async<>(Not UI thread), i used this and its working

MainActivity.this.runOnUiThread(new Runnable() {
                  public void run() {
                      Toast.makeText(getApplicationContext(), "whatever message ",
                           Toast.LENGTH_LONG).show();

hope this help you

jaimin
  • 563
  • 8
  • 25
0

use getApplicationContext() or Activity context it will work

 Toast.makeText(getApplicationContext(), "abc", Toast.LENGTH_LONG).show();
RajaReddy PolamReddy
  • 22,428
  • 19
  • 115
  • 166
0

I've got the problem again today when developing an other app, and did one last search again for that. And finally found that this seems like an Android bug which if you disabled the notifications of the app, toast messages are disabled too.

And all I have to do is go to Settings -> Apps -> My_App check the "Show Notifications" which is unchecked by myself occasionally.

MagicFingr
  • 479
  • 6
  • 20