如何显示土司的AsyncTask在doInBackground [英] How to show toast in AsyncTask in doInBackground
问题描述
在我的活动之一我使用的AsyncTask
。在 doInBackground()
我打电话到各种方法。在这些方法之一,我得到一个例外,所以在catch块我要显示在敬酒的错误。
我知道我可以使用登录
,但我仍然preFER吐司。
那么,怎样才能我doInBackground()使用吐司在AsyncTask的?
In one of my activities I'm using AsyncTask
. In doInBackground()
I'm making calls to various methods. In one of these methods I'm getting an exception, so in the catch block I want to show the error in the Toast.
I know I can use Log
, but still I prefer Toast.
So, how can I use Toast in AsyncTask in doInBackground()?
推荐答案
您可以包装的面包在<一个href="http://developer.android.com/reference/android/app/Activity.html#runOnUiThread%28java.lang.Runnable%29"><$c$c>runOnUIThread()$c$c>但是这不是最好的解决方案。
你应该设置一个布尔标志在catch块发生错误时,则显示 onProgressUpdate适当的吐司()
, onPostExecute()
,或任何其他方法与用户界面访问时的标志真
。
You could wrap the Toast in runOnUIThread()
but this isn't the best solution.
You should set a boolean flag in the catch block when an error occurs, then display an appropriate Toast in onProgressUpdate()
, onPostExecute()
, or any of the other methods with UI access whenever the flag is true
.
这篇关于如何显示土司的AsyncTask在doInBackground的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!