2017-06-13 9 views
3

アプリケーションがクラッシュしてログに記録されていますが、これを修正する方法を見つけようとしていますが、これが原因でContexとPeole 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(); 
    } 

と、このラインcaus Eエラー:

dialog.show(); 

答えて

3

は現在ActivityName.this代わりのthisを追加します。

AlertDialog.Builder dialog = new AlertDialog.Builder(YourCurrentActivityName.this); 
関連する問題