2014-07-17 79 views
0

我有一个来自Google地图的示例代码存在问题! 这段代码在我使用eclipse时起作用。我改变了Android工作室,我有一个奇怪的错误。新错误java.lang.IllegalStateException:onSaveInstanceState后无法执行此操作

错误是:(ⅰ没有方法的onSaveInstanceState)

Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState 
      at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1360) 
      at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1378) 
      at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595) 
      at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574) 
      at android.support.v4.app.DialogFragment.show(DialogFragment.java:138) 
      at com.example.smartoo.HomeActivity.servicesConnected(HomeActivity.java:325) 
      at com.example.smartoo.HomeActivity.stopUpdates(HomeActivity.java:373) 
      at com.example.smartoo.HomeActivity.onStop(HomeActivity.java:209) 
      at android.app.Instrumentation.callActivityOnStop(Instrumentation.java:1212) 
      at android.app.Activity.performStop(Activity.java:5376) 
      at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:3185) 
            at android.app.ActivityThread.handleStopActivity(ActivityThread.java:3234) 
            at android.app.ActivityThread.access$1100(ActivityThread.java:135) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1223) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:136) 
            at android.app.ActivityThread.main(ActivityThread.java:5017) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:515) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
            at dalvik.system.NativeStart.main(Native Method) 

错误是在这条线:

errorFragment.show(getSupportFragmentManager(), LocationUtils.APPTAG); 

的代码:

/** 
    * Verify that Google Play services is available before making a request. 
    * 
    * @return true if Google Play services is available, otherwise false 
    */ 
    private boolean servicesConnected() { 

     // Check that Google Play services is available 
     int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this); 

     // If Google Play services is available 
     if (ConnectionResult.SUCCESS == resultCode) { 
      // In debug mode, log the status 
      Log.d(LocationUtils.APPTAG, getString(R.string.play_services_available)); 

      // Continue 
      return true; 
     // Google Play services was not available for some reason 
     } else { 
      // Display an error dialog 
      Dialog dialog = GooglePlayServicesUtil.getErrorDialog(resultCode, this, 0); 
      if (dialog != null) { 
       ErrorDialogFragment errorFragment = new ErrorDialogFragment(); 
       errorFragment.setDialog(dialog); 
       errorFragment.show(getSupportFragmentManager(), LocationUtils.APPTAG); 
      } 
      return false; 
     } 
    } 

THX!

+0

http://stackoverflow.com/a/10261449和http://stackoverflow.com/a/10261438会给你一个好主意 – Sree

+0

我没有任何交易......我怎么能使用“transaction.commitAllowingStateLoss ();” ? – Maxouille

+0

阅读这两个链接,你可以理解为什么发生问题 – Sree

回答

1

通常情况下,上述代码在onStart/OnResume方法中或在这些方法之后调用,而不是在onStop/onSaveInstance之内。在活动被杀之后,你正在尝试做一些手术。在onStop/finish/saveInstances方法调用之前执行serivcesConnected调用。

+0

我可以在方法OnPause上调用这个吗? – Maxouille

+0

是的。在此之前,在超级方法调用之前。例如: - 在'super.onpause()'前面的'onPause()'里面。同样也有'onstop()'方法。 – vakman

相关问题