java.lang.IllegalArgumentException - dialog.dismiss

时间:2016-09-22 13:15:12

标签: java android illegalargumentexception dismiss

我在发布的应用程序中收到此错误,只有客户端收到此错误。我已经多次试过复制同样的错误但是没有成功。 我也已经尝试在有对话但没有解决的所有位置使用下面的代码。

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

错误报告

java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{16faa139 V.E..... R.....I. 0,0-0,0} not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:412)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:338)
at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:122)
at android.app.Dialog.dismissDialog(Dialog.java:522)
at android.app.Dialog.dismiss(Dialog.java:504)

**at br.my.project.de.a(Unknown Source)
at br.my.project.de.onPostExecute(Unknown Source)**

at android.os.AsyncTask.finish(AsyncTask.java:636)
at android.os.AsyncTask.access$500(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6946)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

4 个答案:

答案 0 :(得分:2)

我可以看到你试图在AsyncTask的postExecute上解除ProgressDialog。这本身就是一个很好的做法,但有时候有些错误,我之前也有过这样的经历,特别是当你显示ProgressDialog并突然旋转视图时。

我发现解决此问题的解决方案如下:

您将需要这些功能来处理正确的解雇并避免崩溃。

private void dismissProgressDialog(ProgressDialog progressDialog) {
            if (progressDialog != null) {
                if (progressDialog.isShowing()) {

                    //get the Context object that was used to create the dialog
                    Context context = ((ContextWrapper) progressDialog.getContext()).getBaseContext();

                    // if the Context used here was an activity AND it hasn't been finished or destroyed
                    // then dismiss it
                    if (context instanceof Activity) {

                        // Api >=17
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                            if (!((Activity) context).isFinishing() && !((Activity) context).isDestroyed()) {
                                dismissWithExceptionHandling(progressDialog);
                            }
                        } else {

                            // Api < 17. Unfortunately cannot check for isDestroyed()
                            if (!((Activity) context).isFinishing()) {
                                dismissWithExceptionHandling(progressDialog);
                            }
                        }
                    } else
                        // if the Context used wasn't an Activity, then dismiss it too
                        dismissWithExceptionHandling(progressDialog);
                }
                progressDialog = null;
            }
        }


public void dismissWithExceptionHandling(ProgressDialog dialog) {
            try {
                dialog.dismiss();
            } catch (final IllegalArgumentException e) {
                // Do nothing.
            } catch (final Exception e) {
                // Do nothing.
            } finally {
                dialog = null;
            }
        }

在AsyncTask的onPostExecute上实现该功能。

@Override
 protected void onPostExecute(Boolean b) {
     // pass in the progressDialog as a parameter to the method
     dismissProgressDialog(progressDialog);
 }

答案 1 :(得分:0)

您正在调用当前未再显示的对话框。如下所示:当您调用dismiss时,您的Activity / Fragment可能已被销毁。

答案 2 :(得分:0)

将此代码写入您的活动

  

的onStop()

方法。当任何人按下主页按钮并且如果打开对话框,则会出现此错误。因为点击主页按钮onPause()和onStop()方法调用。希望这会有所帮助。

 if (dialog!=null && dialog.isShowing ()) {
          dialog.dismiss ();
             }

答案 3 :(得分:0)

修改此代码:View not attached to window manager crash 如何重现错误:

private static void dismissDialog(Activity activity, Dialog dialog) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        if (activity.isDestroyed()) { // or call isFinishing() if min sdk version < 17
            return;
        }
    } else {
        if (activity.isFinishing()) { // or call isFinishing() if min sdk version < 17
            return;
        }
    }
    if (null != dialog && dialog.isShowing()) {
        dialog.dismiss();
    }
}