Java android android.view.WindowManager $ BadTokenException:无法添加窗口

时间:2017-06-13 05:45:42

标签: java android

我的应用程序崩溃,在日志中我看到了这一点,我试图找到我如何解决它,但只有我发现这是因为一个上下文和一个人使用YourActivityName.this并且它工作正常,但我喜欢这个我的应用程序崩溃了

这是日志:

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)

这是代码:

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

并且此行导致错误:

dialog.show();

1 个答案:

答案 0 :(得分:6)

添加当前 ActivityName.this ,而不是 this

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