2

I am facing some problems when I'm using Toast inside run method of Thread class.

My error Logcat

09-16 11:42:38.140: E/AndroidRuntime(1446): in writeCrashedAppName, pkgName :com.monday.worker_android
09-16 11:59:17.920: E/AndroidRuntime(2144): FATAL EXCEPTION: AsyncTask #1
09-16 11:59:17.920: E/AndroidRuntime(2144): java.lang.RuntimeException: An error occured while executing doInBackground()
09-16 11:59:17.920: E/AndroidRuntime(2144):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
09-16 11:59:17.920: E/AndroidRuntime(2144):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
09-16 11:59:17.920: E/AndroidRuntime(2144):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
09-16 11:59:17.920: E/AndroidRuntime(2144):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
09-16 11:59:17.920: E/AndroidRuntime(2144):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
09-16 11:59:17.920: E/AndroidRuntime(2144):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
09-16 11:59:17.920: E/AndroidRuntime(2144):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
09-16 11:59:17.920: E/AndroidRuntime(2144):     at java.lang.Thread.run(Thread.java:1019)
09-16 11:59:17.920: E/AndroidRuntime(2144): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
09-16 11:59:17.920: E/AndroidRuntime(2144):     at android.os.Handler.<init>(Handler.java:121)
09-16 11:59:17.920: E/AndroidRuntime(2144):     at android.widget.Toast.<init>(Toast.java:75)
09-16 11:59:17.920: E/AndroidRuntime(2144):     at android.widget.Toast.makeText(Toast.java:244)
09-16 11:59:17.920: E/AndroidRuntime(2144):     at com.monday.worker_android.SwipePage$2.doInBackground(SwipePage.java:142)
09-16 11:59:17.920: E/AndroidRuntime(2144):     at com.monday.worker_android.SwipePage$2.doInBackground(SwipePage.java:1)
09-16 11:59:17.920: E/AndroidRuntime(2144):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
09-16 11:59:17.920: E/AndroidRuntime(2144):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)

But when I ignore Toast it works fine. But I want to use it. Please help.

Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44

7 Answers7

9

By calling an Activity's runOnUiThread method from your Thread:

activity.runOnUiThread(new Runnable() {
    public void run() {
        Toast.makeText(activity, "Toast message inside Thread", Toast.LENGTH_LONG).show();
    }
});
Jay
  • 1,474
  • 9
  • 13
3

You've to call the Toast on UI thread.

AsyncTask onPostExecute runs on UI thread. If you want to "force" a UI thread, you can use the following code inside onBackground:

// code runs in a thread
   runOnUiThread(new Runnable() {
      @Override
          public void run() {
              Toast.makeText(....).show();
          }
   });
Reinherd
  • 5,476
  • 7
  • 51
  • 88
1

According to your logcat you are trying to update UI part (using Toast) in doInBackground() of AsyncTask which is not possible directly

Use same in onPostExecute() which is used for handling/updating UI Part

If you want to show Toast in doInbackground() wrap the Toast in runOnUIThread() but this isn't the best solution.

like

runOnUiThread(new Runnable() {
      @Override
          public void run() {
              // Show Toast Here
          }
   });
Tarsem Singh
  • 14,139
  • 7
  • 51
  • 71
1

You may display your Toast message as below:

YourActivityName.this.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            Toast.makeText(YourActivityName.this, "This is Toast!!!", Toast.LENGTH_SHORT).show();

        }
    });
1

You can not use or access any UI elements inside a non UI thread. If you want to see some messages about your thread then you may use AsyncTask and use the UI stuffs inside preExecute method.because the pre and postExecute of AsyncTask are run on UI thread.

If you want to do like so then see Problem with Toast in AsyncTask method call .

If you are excepting a small and easy way, then just use LOG class like:

Log.d("String Key", "the value you want to see");

If you want some ideas about log then please see Android Log.v(), Log.d(), Log.i(), Log.w(), Log.e() - When to use each one?

Hope these are sufficient for your question.

Community
  • 1
  • 1
Ranjit
  • 5,130
  • 3
  • 30
  • 66
  • 1
    I already give you some ways and links and also you can see the other answers here like jay ,pratic and sergi to use runOnUiThread.All answers are helpful. – Ranjit Sep 16 '13 at 07:07
1

You can not print Toast in Thread, because Thread is running in background and it will not affect GUI things. You have to do it using UI Thread.

runOnUiThread(new Runnable() {
      @Override
          public void run() {
             Toast.makeText(this, "YOUR MESSAGE TO PRINT", Toast.LENGTH_LONG).show().
          }
   });
Pratik Dasa
  • 7,439
  • 4
  • 30
  • 44
0

You need to call toast inside onPostExecute() method

@Override
protected void onPostExecute(Boolean result)
{
    super.onPostExecute(result);
    Toast.makeText(context, "Message", Toast.LENGTH_SHORT).show();
}
Ashok
  • 1,251
  • 11
  • 19