【发布时间】:2011-09-06 22:08:33
【问题描述】:
我有以下代码:
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 刚刚被跳过一样。
非常感谢您的帮助!
【问题讨论】:
标签: android android-asynctask progressdialog android-ui