【问题标题】:View not attached to window manager, dialog dismiss视图未附加到窗口管理器,对话框关闭
【发布时间】:2013-11-01 12:40:46
【问题描述】:

所以我有一个名为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 有时当我去另一个活动时,我的帖子顶部会出现这样的错误。我不知道如何解决这个问题,它很难调试,因为它很少出现错误但它存在。

【问题讨论】:

标签: android


【解决方案1】:

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

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

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

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

【讨论】:

    【解决方案2】:

    使用 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();
    

    【讨论】:

    • 不知道这有什么帮助,你只是在修补问题(因为处理异常很慢,所以根本没有优化),而不是提供真正的解决方案。
    • 我猜它通过直接修复 OP 的问题/错误有所帮助,你有这个问题的“真正”解决方案吗?
    • 我之前遇到了和发帖人一样的问题,那个答案没有用。我相信发布者和我自己遇到了满足该条件的不同场景,但仍然导致抛出异常。
    • 见 cmets java.lang.IllegalArgumentException: View not attached to window manager。像这样吞下异常是个坏主意。
    【解决方案3】:

    onDestroy()onStop() 方法中取消这样的检查之前..您只是简单地取消而不检查它是否显示

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

    【讨论】:

    • 如果我将这两个语句放在一个条件下会有什么不同吗? if (null != pDialog && pDialog.isShowing()) { pDialog.dismiss(); } -> 这触发了IllegalArgumentException: View not attached to window manager
    • 在我的 Asynctask 的 onPostExecute 内部,我想知道为什么它静止触发了异常。
    • 别忘了super.onDestroysuper.onPause
    【解决方案4】:

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

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

    如果你是懒惰的,其他发帖者关于将它包装在 try {} 中的评论在 /most/ 情况下也确实有效(尽管在少数情况下系统可能会关闭它并且异常仍然会导致强制-关闭你不能像在 android 代码中那样进行 try{} 轮,而不是你的)

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

    【讨论】:

    • "虽然在少数情况下系统可能会关闭它,但异常仍会导致强制关闭,您无法像在 android 代码中那样进行 try{} 轮次,不是你的”一个例子?
    • 这个问题的最佳答案!
    【解决方案5】:

    这样做

    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();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-23
      • 2014-05-20
      • 2012-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-14
      相关资源
      最近更新 更多