【问题标题】:java.lang.IllegalArgumentException - dialog.dismissjava.lang.IllegalArgumentException - dialog.dismiss
【发布时间】:2017-01-31 02:18:42
【问题描述】:

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

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)

【问题讨论】:

  • 对话是在活动还是片段中?当您关闭对话框时,检查它是否为空
  • 嗨@Piyush,对话框在活动中
  • onDestroy()活动方法中检查它是否不为空然后取消对话框并使其对象null
  • @Piyush 好的,我试试看!但它有办法在开发模式下复制这个错误?

标签: java android illegalargumentexception dismiss


【解决方案1】:

我可以看到您正试图在 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);
 }

【讨论】:

    【解决方案2】:

    在你的活动中写下这段代码

    onStop()

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

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

    【讨论】:

    • 不工作。我已经在onPause 中调用了dismiss
    【解决方案3】:

    修改此代码: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();
        }
    }
    

    【讨论】:

      【解决方案4】:

      您正在对当前不再显示的对话框调用解除。如:当你调用dismiss时,你的Activity/Fragment可能已经被销毁了。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-05
        • 1970-01-01
        相关资源
        最近更新 更多