Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

multithreading - How to use toast message inside Thread in Android

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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();
    }
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...