2014-10-29 55 views
1
What could be causing this exception? 

    E/AndroidRuntime(16901): FATAL EXCEPTION: main 
    E/AndroidRuntime(16901): Process: com.borqs.karbonn.music, PID: 16901 
    E/AndroidRuntime(16901): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState 
    E/AndroidRuntime(16901):  at android.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1323) 
    E/AndroidRuntime(16901):  at android.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1341) 
    E/AndroidRuntime(16901):  at android.app.BackStackRecord.commitInternal(BackStackRecord.java:609) 
    E/AndroidRuntime(16901):  at android.app.BackStackRecord.commit(BackStackRecord.java:587) 
    E/AndroidRuntime(16901):  at com.borqs.music.MusicHubMainActivity.onNavigationItemSelected(MusicHubMainActivity.java:1167) 
    E/AndroidRuntime(16901):  at com.android.internal.widget.ActionBarView$1.onItemSelected(ActionBarView.java:148) 
    E/AndroidRuntime(16901):  at android.widget.AdapterView.fireOnSelected(AdapterView.java:893) 
    E/AndroidRuntime(16901):  at android.widget.AdapterView.access$200(AdapterView.java:48) 
    E/AndroidRuntime(16901):  at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:861) 
    E/AndroidRuntime(16901):  at android.os.Handler.handleCallback(Handler.java:808) 
    E/AndroidRuntime(16901):  at android.os.Handler.dispatchMessage(Handler.java:103) 
    E/AndroidRuntime(16901):  at android.os.Looper.loop(Looper.java:193) 
    E/AndroidRuntime(16901):  at android.app.ActivityThread.main(ActivityThread.java:5299) 
    E/AndroidRuntime(16901):  at java.lang.reflect.Method.invokeNative(Native Method) 
    E/AndroidRuntime(16901):  at java.lang.reflect.Method.invoke(Method.java:515) 
    E/AndroidRuntime(16901):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:825) 
    E/AndroidRuntime(16901):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:641)^M 
    E/AndroidRuntime(16901):  at dalvik.system.NativeStart.main(Native Method) 


This is my onSave instance code 

    @Override 
     public void onSaveInstanceState(Bundle state) 
     { 
      super.onSaveInstanceState(state); 
      saveInstance =true; 
      if (state == null) 
      { 
       FragmentManager mFragmentManager = getFragmentManager(); 
       FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction(); 
       MediaPlaybackFragment fragment = new MediaPlaybackFragment(); 
       fragmentTransaction.add(R.id.content_frame, fragment); 
       fragmentTransaction.commit(); 
      } else { 
       state.putInt("position", mPostion); 
       selectedTab = tabHost.getCurrentTab(); 
       state.putInt("tabhostselected", selectedTab); 
       state.putBoolean("isSaveInstance", saveInstance); 
       // state.putBoolean("draweropened", mDrawergarment.isDrawerOpened()); 
      } 
     } 

固定java.lang.IllegalStateException這是我的上導航代碼 @覆蓋 公共布爾onNavigationItemSelected(INT位置,長的itemId){ 片段newFragment = NULL; FragmentManager fragmentManager = getFragmentManager(); FragmentTransaction ft = fragmentManager.beginTransaction(); newFragment = new MediaPlaybackFragment(); ft.replace(R.id.content_frame,newFragment,「Music」); ft.commit(); }如何用的onSaveInstanceState

Any help would be much appreciated, if you need more information please ask. 
+0

並提到線MusicHubMainActivity.java:1167在它。 – 2014-10-29 06:34:44

+0

你正在使用什麼API級別? – Amy 2014-10-29 06:35:29

+0

[這可能有用](http://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html)? – 2014-10-29 06:36:37

回答

2

變化

fragmentTransaction.commit(); 

fragmentTransaction.commitAllowingStateLoss(); 

也許你到別的地方提交您的事務在你的代碼 - 你應該使用commitAllowingStateLoss()有藏漢。但我會建議你重寫你的代碼,以避免提交後onSaveInstanceState()

+0

@ localhost,但根據文檔類似commit(),但允許在活動狀態保存後執行提交。這很危險,因爲如果活動需要稍後從其狀態恢復,那麼提交可能會丟失,所以這應該僅用於UI狀態在用戶意外更改的情況。 – zzz 2014-10-29 08:59:06

+0

@ localhost你對此做了什麼 – zzz 2014-10-29 09:00:24

+0

@zzz,對不起,但我沒有得到你的問題:) – localhost 2014-10-29 09:13:24

相關問題