Java Android android.view.WindowManager$BadTokenException : Unable to add window

Java Android android.view.WindowManager$BadTokenException : Unable to add window … here is a solution to the problem.

Java Android android.view.WindowManager$BadTokenException : Unable to add window

My app crashed, I saw this in the logs and I tried to find a way to fix it, but only I found out that it was because of a context and a person using YourActivityName.this and it worked fine, but I had my app crashed like this

Here is the log:

Fatal Exception: android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@482fc81 is not valid; is your activity running?
       at android.view.ViewRootImpl.setView(ViewRootImpl.java:584)
       at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:310)
       at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:86)
       at android.app.Dialog.show(Dialog.java:322)
       at android.support.v7.app.AlertDialog$Builder.show(AlertDialog.java:953)
       at pl.eltegps.smokkomunikator.ui.activity.MainActivity.runServices(MainActivity.java:505)
       at pl.eltegps.smokkomunikator.ui.activity.MainActivity.onStart(MainActivity.java:316)
       at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1238)
       at android.app.Activity.performStart(Activity.java:6288)
       at android.app.Activity.performRestart(Activity.java:6334)
       at android.app.ActivityThread.handleSleeping(ActivityThread.java:3688)
       at android.app.ActivityThread.access$2900(ActivityThread.java:157)
       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1531)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:148)
       at android.app.ActivityThread.main(ActivityThread.java:5527)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)

Here is the code:

 protected void runServices() {
        if (LocationUtil.isLocationEnabled(getBaseContext())) {
            if (checkPermission()) {
                runLocationService();
            }
        } else {
            AlertDialog.Builder dialog = new AlertDialog.Builder(this);
            dialog.setMessage(getResources().getString(R.string.location_settings_disabled));
            dialog.setPositiveButton(getString(R.string.go_to_location_settings), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                    App.getBus().post(new PageNavigateEvent(Const.APP_SETTINGS_LOCATION));
                }
            });
            dialog.setNegativeButton(getResources().getString(R.string.cancel), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface paramDialogInterface, int paramInt) {
                    finish();
                }
            });
            dialog.show();
        }
        runTrackerService();
        runConfigService();
    }

This line results in an error:

dialog.show();

Solution

Add the current ActivityName.this instead of this.

 AlertDialog.Builder dialog = new AlertDialog.Builder(YourCurrentActivityName.this);

Related Problems and Solutions