2015-09-29 76 views
5

MainActivity中有一個FragmentTabHost,並且有五個Fragments添加到此FragmentTabHost。 這裏是初始化代碼java.lang.IllegalStateException:在onSaveInstanceState,FragmentTabHost之後無法執行此操作

protected void onCreate(Bundle savedInstanceState) { 
     mFragmentManager = getSupportFragmentManager(); 
     mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); 
     mTabHost.setup(this, mFragmentManager, android.R.id.tabcontent); 
     // Home 
     mTabHost.addTab(
       mTabHost.newTabSpec(TAB_TAG_HOME).setIndicator(
         newTabItem(R.drawable.mi_selector_tab_home, 
           R.string.mi_tab_item_milian)), 
       TabFateFragment.class, null); 

     // Serach 
     mTabHost.addTab(
       mTabHost.newTabSpec(TAB_TAG_SEARCH).setIndicator(
         newTabItem(R.drawable.mi_selector_tab_search, 
           R.string.mi_navi_title_search)), 
       TabSearchFragment.class, null); 

     // Msg 
     mTabHost.addTab(
       mTabHost.newTabSpec(TAB_TAG_MSG).setIndicator(
         newTabItem(R.drawable.mi_selector_tab_msg, 
           R.string.mi_navi_title_msg)), 
       TabMsgFragment.class, null); 

     // Nearby 
     mTabHost.addTab(
       mTabHost.newTabSpec(TAB_TAG_NEARBY).setIndicator(
         newTabItem(R.drawable.mi_selector_tab_nearby, 
           R.string.mi_navi_title_nearby)), 
       TabNearbyFragment.class, null); 

     // Me 
     mTabHost.addTab(
       mTabHost.newTabSpec(TAB_TAG_ME).setIndicator(
         newTabItem(R.drawable.mi_selector_tab_me, 
           R.string.mi_navi_title_me)), 
       TabMeFragment.class, null); 
     ...... 
} 

而且我已閱讀本article,發現沒有FragmentTransaction#提交()在我的代碼。

我試圖在我的Activity.onSaveInstanceState沒有要求super.onSaveInstanceState(捆綁outState)(outState)

@Override 
    protected void onSaveInstanceState(Bundle outState) { 
     // super.onSaveInstanceState(outState); 
    } 

這裏的崩潰日誌

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState 
    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(Unknown Source) 
    at android.support.v4.app.FragmentManagerImpl.enqueueAction(Unknown Source) 
    at android.support.v4.app.BackStackRecord.commitInternal(Unknown Source) 
    at android.support.v4.app.BackStackRecord.commit(Unknown Source) 
    at android.support.v4.app.FragmentTabHost.onAttachedToWindow(Unknown Source) 
    at android.view.View.dispatchAttachedToWindow(View.java:12134) 
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2453) 
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2460) 
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2460) 
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2460) 
    at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2460) 
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1207) 
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004) 
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5483) 
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:778) 
    at android.view.Choreographer.doCallbacks(Choreographer.java:591) 
    at android.view.Choreographer.doFrame(Choreographer.java:561) 
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:764) 
    at android.os.Handler.handleCallback(Handler.java:730) 
    at android.os.Handler.dispatchMessage(Handler.java:92) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:5109) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:525) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
    at dalvik.system.NativeStart.main(Native Method) 
+0

上述代碼是否在您的活動的onCreate()內執行?你打電話給setup()嗎? – logcat

+0

我忘了將它粘貼到我的代碼中,mTabHost.setup(this,mFragmentManager,android.R.id.tabcontent); – Scorpiuszjj

+0

我遇到了同樣的問題。你有什麼解決方案嗎? – MobileAppDeveloper

回答

3

的onSaveInstanceState的米ethod在Activity即將被銷燬之前被調用,以保存Activity數據。在數據保存之後,會導致錯誤添加Fragment。解決方法是用commitAllowingStateLoss()替換commit()方法,是一樣的。

+0

但是,我的代碼中沒有FragmentTransaction#commit()。 – Scorpiuszjj

+0

您可以放棄使用FragmentTabHost,而不是其他方法,就像我的新答案一樣。 – John

+0

我只是想弄清楚這一點,而不是簡單地嘗試另一種實現。 – Scorpiuszjj

-1

使用的FrameLayout:

<FrameLayout 

      android:id="@+id/tab_content" 

      android:layout_width="match_parent" 

      android:layout_height="match_parent" 

      android:layout_gravity="bottom" 

      android:orientation="vertical" /> 

,並添加了一個方法:

private synchronized void replaceFragment() { 



     FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction(); 

     // Workaround for Issue 42601: 

     // https://code.google.com/p/android/issues/detail?id=42601 

     fragmentTransaction.detach(tabFragments[previousTabIdx]).replace(R.id.tab_content, tabFragments[curTabIdx]) 

       .attach(tabFragments[curTabIdx]).commitAllowingStateLoss(); 

     previousTabIdx = curTabIdx; 

    } 

那麼你可以使用commitAllowingStateLoss()方法。

相關問題