Android AsyncTask - onPostExecute 未调用(ProgressDialog)
我有以下代码:
try {
res = new Utils(ubc_context).new DownloadCalendarTask().execute().get();
} catch (InterruptedException e) {
Log.v("downloadcalendar", "interruptedexecution : " + e.getLocalizedMessage());
res = false;
} catch (ExecutionException e) {
Log.v("downloadcalendar", "executionexception : " + e.getLocalizedMessage());
res = false;
}
Log.v("displaymenu", "A");
public class Utils {
private Context context;
public Utils(Context context) {
this.context = context;
}
public class DownloadCalendarTask extends AsyncTask<Void, Void, Boolean> {
private ProgressDialog dialog;
public DownloadCalendarTask() {
dialog = new ProgressDialog(context);
}
protected void onPreExecute() {
Log.v("preexecute", "A");
dialog.setMessage("Loading calendar, please wait...");
Log.v("preexecute", "B");
dialog.show();
Log.v("preexecute", "C");
}
protected Boolean doInBackground(Void... arg0) {
// do some work here...
return (Boolean) false;
}
protected void onPostExecute() {
Log.d("utils", "entered onpostexecute");
dialog.dismiss();
}
}
}
代码的第一部分附加到按钮的 onClick
侦听器。当我单击按钮时,按钮会闪烁(这表明它已被单击),然后大约 8 秒后出现“正在加载”对话框,但从未完成。
根据logcat,一旦我点击按钮onPreExecute
就会像Dialog.show()
一样执行,所以我的第一个问题是为什么会有8秒的延迟?在这 8 秒内,logcat 显示正在执行 doInBackground
。然而,根据 logcat (这是第二个问题), onPostExecute 永远不会被调用(因此 Dialog.dismiss() )永远不会运行。
Logcat 显示 DownloadCalendarTask().execute().get()
之后的所有内容都正在执行,因此就好像 onPostExecute
刚刚被跳过。
非常感谢您的帮助!
I have the following code:
try {
res = new Utils(ubc_context).new DownloadCalendarTask().execute().get();
} catch (InterruptedException e) {
Log.v("downloadcalendar", "interruptedexecution : " + e.getLocalizedMessage());
res = false;
} catch (ExecutionException e) {
Log.v("downloadcalendar", "executionexception : " + e.getLocalizedMessage());
res = false;
}
Log.v("displaymenu", "A");
public class Utils {
private Context context;
public Utils(Context context) {
this.context = context;
}
public class DownloadCalendarTask extends AsyncTask<Void, Void, Boolean> {
private ProgressDialog dialog;
public DownloadCalendarTask() {
dialog = new ProgressDialog(context);
}
protected void onPreExecute() {
Log.v("preexecute", "A");
dialog.setMessage("Loading calendar, please wait...");
Log.v("preexecute", "B");
dialog.show();
Log.v("preexecute", "C");
}
protected Boolean doInBackground(Void... arg0) {
// do some work here...
return (Boolean) false;
}
protected void onPostExecute() {
Log.d("utils", "entered onpostexecute");
dialog.dismiss();
}
}
}
The first part of code is attached to an onClick
listener for a button. When I click the button the button flashes (as it does to show it has been clicked), and then after about 8 seconds the Loading dialog appears but never finishes.
According to logcat, as soon as I click the button onPreExecute
is executed as is Dialog.show()
, so my first problem is why is there this 8 second delay? During these 8 seconds, logcat shows that doInBackground
is being executed. However, according to logcat (this is the second problem) onPostExecute is never called (and so Dialog.dismiss()) is never run.
Logcat shows that everything following DownloadCalendarTask().execute().get()
is being executed, so it's as if onPostExecute
has just been skipped.
Many thanks for your help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您调用 AsyncTask.get() 会导致 UI 线程在 AsyncTask 执行时被阻塞。
如果删除对 get() 的调用,它将异步执行并给出预期结果。
编辑:
您还需要更新 onPostExecute 方法的参数,它们需要包含结果。例如
You are calling AsyncTask.get() which causes the UI thread to be blocked while the AsyncTask is executing.
If you remove the call to get() it will perform asynchronously and give the expected result.
Edit:
You will also need to update the parameters to your onPostExecute method, they need to include the result. e.g.