在我的一项 Activity 中,我使用了 AsyncTask
。在 doInBackground()
中,我正在调用各种方法。在其中一种方法中,我遇到了异常,因此在 catch block 中,我想在 Toast 中显示错误。
我知道我可以使用 Log
,但我仍然更喜欢 Toast。
那么,如何在 doInBackground() 中的 AsyncTask 中使用 Toast?
最佳答案
从 doInBackground 返回
protected String doInBackground(String... params){
//some code
try{
//some code
}catch(Exception e){
return "Exception Caught";
}
return someValidResult;
}
protected void onPostExecute(String result){
if(result.equalsIgnoreCase("Exception Caught")){
//Display Toast
}else{
// // whatever you wana do with valid result
}
}
关于android - 如何在 doInBackground 中的 AsyncTask 中显示 toast,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13790351/