如何在Android中使用敬酒消息内螺纹 [英] How to use toast message inside Thread in Android

查看:134
本文介绍了如何在Android中使用敬酒消息内螺纹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我,当我使用吐司Thread类的run方法内面临的一些问题。

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

我的错误的logcat

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.

推荐答案

您不能使用或访问非UI线程内的任何UI元素。如果你想看到你的线程一些消息,那么你可以使用的AsyncTask,并使用preExecute里面的东西UI的method.because pre和的AsyncTask的postExecute是在UI线程上运行。

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.

如果你想要做像这样再看看<一个href=\"http://stackoverflow.com/questions/5747645/problem-with-toast-in-asynctask-method-call\">Problem在AsyncTask的方法调用吐司。

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

如果您是一个除外小而简单的方式,那么就使用Log类,如:

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");

如果你想了解日志的一些想法,那么请参阅<一个href=\"http://stackoverflow.com/questions/7959263/android-log-v-log-d-log-i-log-w-log-e-when-to-use-each-one\">Android Log.v(),Log.d(),Log.i(),Log.w(),Log.e() - 何时使用每个

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.

这篇关于如何在Android中使用敬酒消息内螺纹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆