2017-06-02 60 views
0

我正在使用SupportMapFragment,並在提交時崩潰且出現非法狀態異常。 這裏的代碼如下:setMap上的onSaveInstanceState後無法執行此操作

mSupportMapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.branch_mp_view); 
     if (mSupportMapFragment == null) { 
      FragmentManager fragmentManager = getChildFragmentManager(); 
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
      mSupportMapFragment = SupportMapFragment.newInstance(); 
      fragmentTransaction.replace(R.id.branch_mp_view, mSupportMapFragment).commit(); 
     } 

     if (mSupportMapFragment != null) { 
      mSupportMapFragment.getMapAsync(this); 
     } 

日誌:

Fatal Exception: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState 
     at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1538) 
     at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1556) 
     at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:696) 
     at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:662) 
     at com.android.myapp.ui.branch_master.MyFragment.setMap(MyFragment.java:1346) 
     at com.android.myapp.ui.branch_master.MyFragment.handleSuccessBranchDetail(MyFragment.java:3267) 
     at com.android.myapp.ui.branch_master.MyFragment$1.onResponse$230aa0b4(MyFragment.java:200) 
     at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68) 
     at android.os.Handler.handleCallback(Handler.java:815) 
     at android.os.Handler.dispatchMessage(Handler.java:104) 
     at android.os.Looper.loop(Looper.java:194) 
     at android.app.ActivityThread.main(ActivityThread.java:5637) 
     at java.lang.reflect.Method.invoke(Method.java) 
     at java.lang.reflect.Method.invoke(Method.java:372) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:959) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:754) 

請幫助,我無法重現它,但我現在在crashlytics看到這個崩潰在一段時間出現一次。

+0

[java.lang.IllegalStateException:onSaveInstanceState後無法執行此操作]的可能重複(https://stackoverflow.com/questions/14177781/java-lang-illegalstateexception-can-not-perform-this-action-後onsaveinstanc) – azizbekian

回答

0

看起來你正試圖在apicall完成後顯示mapFragment。與此同時,用戶可能已經關閉了應用程序,或者去了另一個屏幕。當發生這種情況時,活動或片段會暫停。暫停後不可能再替換片段。你可以通過檢查你的ap是否還在前臺來防止這種情況發生。例如,當調用onResume時,可以將名爲foreground的布爾值設置爲true,並在調用onPause時將其設置爲false。您現在可以使用此變量來查看您的片段是否可以顯示。

相關問題