So I\'ve activity called GameActivity.java
and in this activity I call DialogAnswer.show()
which simple shows some picture on screen.
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();
}
}
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;
}
}
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.
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();
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();