视图未附加到窗口管理器,对话框关闭



所以我有一个名为GameActivity.java的活动,在这个活动中,我称之为DialogAnswer.show(),它简单地在屏幕上显示一些图片。

java.lang.IllegalArgumentException: View not attached to window manager
at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:402)
at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:304)
at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:79)
at android.app.Dialog.dismissDialog(Dialog.java:325)
at android.app.Dialog.dismiss(Dialog.java:307)
at pl.evelanblog.prawdaczyfalsz.screen.DialogAnswer$1.onFinish(DialogAnswer.java:36)
at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:118)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5328)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)

这是我DialogAnswer.java

public class DialogAnswer extends Activity {
   private static ImageView resultImage;
   private static Dialog dialog = null;
   public static void show(Context context, boolean fCorrect) {
       dialog = new Dialog(context, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
       dialog.setContentView(R.layout.dialog);
       resultImage = (ImageView) dialog.findViewById(R.id.result_image);
       if (fCorrect)
            resultImage.setImageResource(R.drawable.correct_image);
       else
            resultImage.setImageResource(R.drawable.incorrect_image);
       dialog.show();
        new CountDownTimer(700, 100) {
            public void onTick(long millisUntilFinished) {
            }
            public void onFinish() {
               dialog.dismiss(); //this is line 36
            }
        }.start();
        }
}

GameActivity.java有时当我去另一个活动时,我会在我的帖子顶部收到这样的错误。我不知道如何解决这个问题,很难调试,因为它很少出错,但它存在。

很多人可能会在谷歌上搜索这个,所以我可能会把我的 2p 放进去:

不幸的是,人们使用 isShowing() 的示例不起作用,因为当视图分离时,这仍然可以返回 true(活动已经消失)。

如果你很懒,其他海报评论说把它包装在 try {} 中也确实适用于/most/情况(尽管在某些情况下系统可能会关闭它,并且异常仍然会导致强制关闭,你不能把 try{} 轮,因为它发生在 android 代码中,而不是你的)

最好的解决方案是在活动完成/关闭时关闭对话框。 如果您在异步任务运行时用户导航离开后尝试关闭它(或者,电话响铃并为他们导航),则您将获得 ViewNotAttached 异常。

onDestroy()onStop()方法中关闭这样的检查之前。你只是简单地忽略不检查它是否显示

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

使用 try 语句。

new CountDownTimer(700, 100) {
        public void onTick(long millisUntilFinished) {
        }
        public void onFinish() {
           try {
               dialog.dismiss();
               dialog = null;
          } catch (Exception e) {
               //TODO: Fill in exception
          }
        }
    }.start();
这样做

new CountDownTimer(700, 100) {
            public void onTick(long millisUntilFinished) {
            }
            public void onFinish() {
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        dialog.dismiss(); //this is line 36
                    }
                });
            }
        }.start();

使用 try catch 可能不是解决此问题的有效方法,因为可能会导致内存泄漏;对于这个问题,由于上下文用作参数,因此在使用代码dialog.dismiss之前,我们可以使用以下代码来保护:

public void onFinish() {
   if{ctx instanceof Activity && !((Activity)ctx.isfinishing()){
           dialog.dismiss(); //this is line 36
   }
}

此外,可以使用另一种方法来修补此崩溃在活动的 onDestroy() 函数中,添加代码:

protected void onDestroy() {
  if(dialog != null){
     dialog.dismiss();
     dialog = null;
  }
}

最新更新