我有一个这样的活动:
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"