如何显示土司的AsyncTask在doInBackground土司、AsyncTask、doInBackground

2023-09-13 01:32:15 作者:簡簡單單

在我的活动之一我使用的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()?

推荐答案

您可以包装的面包在runOnUIThread()但是这不是最好的解决方案。 你应该设置一个布尔标志在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.