3

My application is crash and in logs I see this, I try to find how I can fix it , but only I find that is cause a contex and a peole use YourActivityName.this and it works fine , but I have like this and my applications is crash

This is logs :

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 .ui.activity.MainActivity.runServices(MainActivity.java:505)
       at .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)

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

and this line cause errors :

dialog.show();

1 Answer 1

6

Add Current ActivityName.this instead of this.

 AlertDialog.Builder dialog = new AlertDialog.Builder(YourCurrentActivityName.this);
Sign up to request clarification or add additional context in comments.

1 Comment

I pass "this" as Context through my own method and it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.