如何在doInBackground的AsyncTask中显示toast [英] How to show toast in AsyncTask in doInBackground

查看:23
本文介绍了如何在doInBackground的AsyncTask中显示toast的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一项活动中,我使用了 AsyncTask.在 doInBackground() 中,我正在调用各种方法.在这些方法之一中,我遇到了异常,因此在 catch 块中,我想在 Toast 中显示错误.我知道我可以使用 Log,但我仍然更喜欢 Toast.那么,如何在 doInBackground() 的 AsyncTask 中使用 Toast?

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()?

推荐答案

您可以将 Toast 包装在 runOnUIThread() 但这不是最好的解决方案.
当发生错误时,您应该在 catch 块中设置一个布尔标志,然后在 onProgressUpdate()onPostExecute() 或任何其他方法中显示适当的 Toast标志为 true 时的 UI 访问.

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.

这篇关于如何在doInBackground的AsyncTask中显示toast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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