Skip to content Skip to sidebar Skip to footer

View Not Attached To Window Manager, Dialog Dismiss

So I've activity called GameActivity.java and in this activity I call DialogAnswer.show() which simple shows some picture on screen. java.lang.IllegalArgumentException: View not a

Solution 1:

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.

Solution 2:

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

Solution 3:

Use a try statement.

new CountDownTimer(700, 100) {
        publicvoidonTick(long millisUntilFinished) {
        }
        publicvoidonFinish() {
           try {
               dialog.dismiss();
               dialog = null;
          } catch (Exception e) {
               //TODO: Fill in exception
          }
        }
    }.start();

Solution 4:

DO this way

newCountDownTimer(700, 100) {
            publicvoidonTick(long millisUntilFinished) {
            }
            publicvoidonFinish() {
                runOnUiThread(newRunnable() {

                    @Overridepublicvoidrun() {
                        dialog.dismiss(); //this is line 36

                    }
                });
            }
        }.start();

Solution 5:

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:

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

Post a Comment for "View Not Attached To Window Manager, Dialog Dismiss"