Can’t toast on a thread that has not called Looper.prepare()

You CANNOT show a Toast on non-UI thread. You need to call Toast.makeText() (and most other functions dealing with the UI) from within the main thread.


You could use Activity#runOnUiThread():

runOnUiThread(new Runnable() {
      public void run() {
         final Toast toast = Toast.makeText(context, "GAME OVER!\nScore: " + score, duration);
         toast.show();
      }
 });

If you want execute a instrumentation test on main thread, add @UiThreadTest annotation:

@Test
@UiThreadTest
public void useAppContext() {
    // ...
}

P.s: There are also many other ways with explain (using Handler, Looper, Observable..) in these posts: Android: Toast in a thread and Can’t create handler inside thread that has not called Looper.prepare()

Leave a Comment