View not attached to window manager, dialog dismiss

后端 未结 5 812
北海茫月
北海茫月 2020-12-29 09:50

So I\'ve activity called GameActivity.java and in this activity I call DialogAnswer.show() which simple shows some picture on screen.



        
相关标签:
5条回答
  • 2020-12-29 10:15

    Before dismissing check like this in onDestroy() or onStop() method..You are simple dismissing not checking whether it is showing or not

    if (mDialog!=null) {
        if (mDialog.isShowing()) {
            mDialog.dismiss();       
        }
    }
    
    0 讨论(0)
  • 2020-12-29 10:22

    using try catch may not be a efficient way to solve this problem as may cause memory leak; for this question, as the context is used as param, so before using the code dialog.dismiss, we can use codes below to protect:

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

    also, another method can be used to patch this crash in the function of onDestroy() in the activity, add the code:

    protected void onDestroy() {
      if(dialog != null){
         dialog.dismiss();
         dialog = null;
      }
    }
    
    0 讨论(0)
  • 2020-12-29 10:27

    A lot of people may be googling this so I might put my 2p in:

    Unfortunately the examples where people are using isShowing() aren't going to work as this can still return true when the view is detached (the activity has gone).

    If you are lazy, the other posters comment about wrapping it in a try {} does also work in /most/ situations (though there are a few cases where the system may close it and an exception will still result in a force-close that you can't put a try{} round as it happens in android code, not yours)

    The best solution is to close the dialogs when your activity finishes/closes. If you attempt to close it after the user navigates away whilst your async task is running (or, the phone rings and it's navigated away for them) then you're going to get the ViewNotAttached exception.

    0 讨论(0)
  • 2020-12-29 10:32

    DO this way

    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();
    
    0 讨论(0)
  • 2020-12-29 10:34

    Use a try statement.

    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();
    
    0 讨论(0)
提交回复
热议问题