我有这样的活动:
public class StudentActivity extends Activity {
static final int DIALOG_PROGRESS = 0;
public void btnSyncStudentsClick(View view) {
syncStudents();
}
@Override
protected Dialog onCreateDialog(int id) {
Dialog d = null;
switch (id) {
case DIALOG_PROGRESS:
ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage(getText(R.string.txt_refreshing_students));
d = dialog;
break;
return d;
}
private class RefreshStudentTask extends
AsyncTask<Void, Integer, AccountRefreshResult> {
@Override
protected void onPreExecute() {
showDialog(DIALOG_PROGRESS);
}
@Override
protected AccountRefreshResult doInBackground(Void... params) {
//some network requests
}
@Override
protected void onPostExecute(AccountRefreshResult result) {
dismissDialog(DIALOG_PROGRESS); //this throws an exception
//some other stuff
}
}
}
由于对话框显示在onPreExecute()中并在onPostExecute()中被解雇,我不知道为什么有些用户会出现异常:
Uncaught exception handled :/ , thread: Thread[main,5,main]
java.lang.IllegalArgumentException: no dialog with id 0 was ever shown via Activity#showDialog
at android.app.Activity.missingDialog(Activity.java:2606)
at android.app.Activity.dismissDialog(Activity.java:2591)
at pl.mobireg.eparent.activities.StudentActivity$RefreshStudentTask.onPostExecute(StudentActivity.java:390)
at pl.mobireg.eparent.activities.StudentActivity$RefreshStudentTask.onPostExecute(StudentActivity.java:364)
at android.os.AsyncTask.finish(AsyncTask.java:417)
at android.os.AsyncTask.access$300(AsyncTask.java:127)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3691)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
at dalvik.system.NativeStart.main(Native Method)
我不能在我这边复制这个问题,所以很难调试。
编辑: 活动应仅以纵向模式运行:
android:screenOrientation="portrait"
答案 0 :(得分:1)
您的对话已被驳回。这是一种适度普遍的竞争条件。只需抓住异常并继续前进,结果就是两种方式相同 - 对话框消失了。
答案 1 :(得分:1)
您可以通过执行任何启动对话框然后更改方向来复制此操作,因为活动将在没有对话框的情况下重新创建。这是处理对话的许多烦恼之一,因此,Google建议您使用DialogFragment
,因为它会为您管理很多这些边缘情况。