2017-06-13 100 views
3

我的應用程序崩潰,在日誌中我看到這個,我試圖找到我如何解決它,但只有我發現這是導致一個情感和一個人使用YourActivityName.this並能正常工作,但我喜歡這個,我的應用是崩潰

這是日誌:

Fatal Exception: android.view.WindowManager$BadTokenException: Unable to add window -- token [email protected] 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(); 
    } 

這行CAU的故障E:

dialog.show(); 

回答

3

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

AlertDialog.Builder dialog = new AlertDialog.Builder(YourCurrentActivityName.this); 
相關問題