ProgressDialog.dismiss() 显示时出现 IllegalArgumentException

IllegalArgumentException for ProgressDialog.dismiss() when it is showing

这是简单的代码:

if (myProgressDialog.isShowing()) {
    myProgressDialog.dismiss();
}

谁能解释一下为什么 myProgressDialog.dismiss() 有时会导致以下异常:

java.lang.IllegalArgumentException: View=com.android.internal.policy.PhoneWindow$DecorView{83559f5 V.E...... R......D 0,0-1026,348} not attached to window manager
Stack trace: java.lang.IllegalArgumentException: View=com.android.internal.policy.PhoneWindow$DecorView{83559f5 V.E...... R......D 0,0-1026,348} not attached to window manager
        at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:424)
        at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:350)
        at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:116)
        at android.app.Dialog.dismissDialog(Dialog.java:362)
        at android.app.Dialog.dismiss(Dialog.java:345)

试试这个固定代码:

 if (YourActivity.this.isDestroyed()) { // or call isFinishing() if min sdk version < 17
    return;
 } 
 if (myProgressDialog != null && myProgressDialog.isShowing()) {
    myProgressDialog.dismiss();
 }

参考:

这是因为你的activity被摧毁了。 在调用进度对话框之前,请检查您是否在 class 中调用了 finish()。 谢谢