One user sent me this crash report yesterday:
android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@4250d6d8 is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:698)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:326)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
at android.view.Window$LocalWindowManager.addView(Window.java:552)
at android.app.Dialog.show(Dialog.java:277)
at android.app.AlertDialog$Builder.show(AlertDialog.java:932)
at android.webkit.CallbackProxy.handleMessage(CallbackProxy.java:833)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4867)
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:1007)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
at dalvik.system.NativeStart.main(Native Method)
So i searched and it is said that the solution is to change this
to Activity.this
But this solution didnt work even if i use the following code:
//Oncreate Method.....
button.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
AlertDialog.Builder alertd = new AlertDialog.Builder(MainActivity.this);
alertd.setMessage("StackOverflow");
alertd.setTitle("Example");
AlertDialog alertd2 = alertd.create();
alertd2.show();
}
});
Should i change something?
Finally, i found the solution. Problem is that, your alert tries to show, although your activity is finished.
So what you should do is to check if activity is finishing before showing alert. For this purpose isFinishing() method is defined within Activity class.
Here is what you shoul do:
if(!isFinishing())
{
alert.show();
}
Have fun.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With