-1

Possible Duplicate:
Android: Toast in a thread

I'm trying to show a Toast inside a Thread using the standard method:

Toast.makeText(context, "Hello World!", Toast.LENGTH.SHORT).show();

In normal way, this is ofcourse working. But when I try to call from a separate thread I have an exception:

java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()

I have absolutely no clue of what it means.

The context value I pass is the Application itself (an Application inherits from a Context) or an Application.getApplicationContext().

By the way, I've also absolutely no idea why there is this redundant thing. Application (which is by itself already a Context) and Application.getApplicationContext().

Community
  • 1
  • 1
Igor Kravtchenko
  • 49
  • 1
  • 1
  • 6
  • 2
    Same question as here : http://stackoverflow.com/questions/3134683/android-toast-in-a-thread?rq=1 – Thordax Jan 23 '13 at 22:36
  • 1
    The error means Toast messages require a callback to remove the Toast in the future, but this Thread is not able to do that yet. You could prepare and start the Looper in this Thread so you call use callbacks, _but then_ you'll discover that you cannot interact with the UI in a different Thread... – Sam Jan 23 '13 at 22:42
  • 1
    -1 since "[this] question does not show any research effort." [Googling the title](https://www.google.com/search?q=Show+a+Toast+inside+an+Android+thread) would have gotten you your answer. – Cat Jan 23 '13 at 23:03
  • Not any answer found dear Sir. The only is the fact to execute the code inside the thread UI, that makes it impossible to compute any things that would take too much time. And by a matter of fact, break the advantage of being a Thread. – Igor Kravtchenko Jan 23 '13 at 23:45

2 Answers2

8

You can't do anything that affects the UI from worker threads - that includes showing toasts. Use Activity.runOnUiThread() to execute that line on the main thread. Like this:

MyActivity.runOnUiThread(new Runnable()
{
    public void run()
    {
        Toast.makeText(context, "Hello World!", Toast.LENGTH.SHORT).show();
    }
});

Or use a Handler object that's created on the main thread and its post() method. Very similar syntax.

Seva Alekseyev
  • 59,826
  • 25
  • 160
  • 281
  • I suppose you meant Activity.runOnUiThread() ? Anyway, this works but it's not acceptable since I block the UI thread too long. – Igor Kravtchenko Jan 23 '13 at 23:21
  • Right. But I don't get what does blocking have to do with it. Toast is not a progress indicator - it disappears by itself after a timeout. Toast.makeText/show does not block, it returns right away. The blocking action may continue on the thread. – Seva Alekseyev Jan 23 '13 at 23:23
  • I absolutely don't see the interest of this method. It will call the run() method and then? I can create any dummy() method and call it inside my activity as well. The interest of a Thread would have been the possibility to make long computation or such. – Igor Kravtchenko Jan 23 '13 at 23:41
  • Why do you need a toast in the first place? To indicate that a long computation has just started? Then show the toast from the same main thread code that *starts* the worker thread, as opposed to inside the thread. – Seva Alekseyev Jan 23 '13 at 23:50
  • I used this solution but my toast takes 3 hours to disappear, I don't know what I'm doing wrong – Roman Panaget Apr 05 '15 at 23:55
0

You can't manipulate the UI directly from a background thread. One solution is to use a Handler object to communicate between the threads and have it show the Toast for you. Something like this:

Handler h = new Handler() {
  public void handleMessage(Message msg){
    if(msg.what == 0){
      Toast.makeText(context, "HelloWorld!", Toast.LENGTH_SHORT).show();
    }
  }
};

then inside your background thread you can use this:

h.sendEmptyMessage(0);

To send the signal that you are ready to show the Toast.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156