1

我有一个这样的活动:

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"
4

2 回答 2

1

您的对话框已关闭。这是一个比较常见的竞争条件。只需捕获异常并继续前进,结果都是一样的——对话框消失了。

于 2013-03-15T17:09:24.813 回答
1

您可以通过执行任何启动对话框然后更改方向来复制此操作,因为活动将在没有对话框的情况下重新创建。这是处理对话的众多烦恼之一,因此,Google 建议您改用DialogFragment它,因为它为您管理了很多这些边缘情况。

于 2013-03-15T17:11:22.753 回答