2013-09-30 53 views
15

首先清除了指數,我的應用程序具有的結構是這樣的:java.lang.IllegalStateException:失敗節能狀態:活躍在片段

SpashActivity -> MainActivity -> switching between many fragments 

我的應用程序使用SlideMenu到片段之間切換。我必須使用attach而不是replace來保持片段狀態。它看起來像:

public void switchContent(int index, String fragmentTag) {     
    FragmentManager fragmentManager = getSupportFragmentManager(); 
    FragmentTransaction transaction = fragmentManager.beginTransaction(); 
    if (fragmentManager.findFragmentByTag(fragmentTag) != mContent) { 
     if (!mContent.isDetached()) { 
      transaction.detach(mContent); 
     } 
     if (null == fragmentManager.findFragmentByTag(fragmentTag)) { 
      switch (index) { 
      case 0: 
       mContent = new CategoryFragment(); 
       break; 
      case 1: 
       mContent = new BookFragment(); 
       break; 
      case 2: 
       mContent = new BookDetailFragment(); 
       break; 
      // etc 
      } 
     } else { 
       mContent = fragmentManager.findFragmentByTag(fragmentTag);    
     } 

     if (mContent.isDetached()) { 
      transaction.attach(mContent); 
     } else if (!mContent.isAdded()) { 
      transaction.add(R.id.content_frame, mContent, fragmentTag); 
     } 

     transaction.addToBackStack(null); 
     transaction.commit(); 
     fragmentManager.executePendingTransactions(); 
    } 
    Handler h = new Handler(); 
    h.postDelayed(new Runnable() { 
     public void run() { 
      getSlidingMenu().showContent(); 
     } 
    }, 50); 
} 

當回棧計數下降到零,我的應用程序必須退出:

@Override 
public void onBackPressed() { 
    if (0 == getSupportFragmentManager().getBackStackEntryCount()) { 
     // show confirm exit dialog. 
     // If user press ok, try to exit, the splash activity will call finish() 
     // if(dialog == ok){ 
      Intent intent = new Intent(MainActivity.this, SplashActivity.class); 
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
      intent.putExtra(SplashActivity.EXIT_KEY, true); 
      startActivity(intent); 
     // } 
    } else { 
     super.onBackPressed(); 
    } 
} 

有時(不總是)我得到異常和崩潰我的應用程序時,用戶按回很多次(甚至是確認退出對話框不顯示 - 意味着後退堆棧數大於零):

09-30 20:32:53.419: E/AndroidRuntime(796): Uncaught handler: thread main exiting due to uncaught exception 
    09-30 20:32:53.470: E/AndroidRuntime(796): java.lang.RuntimeException: Unable to pause activity {com.org.scgroup/com.org.scgroup.MainActivity}: java.lang.IllegalStateException: Failure saving state: active BookDetailFragment{43e7e0d8} has cleared index: -1 
    09-30 20:32:53.470: E/AndroidRuntime(796): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3162) 
    09-30 20:32:53.470: E/AndroidRuntime(796): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3119) 
    09-30 20:32:53.470: E/AndroidRuntime(796): at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3102) 
    09-30 20:32:53.470: E/AndroidRuntime(796): at android.app.ActivityThread.access$2400(ActivityThread.java:119) 
    09-30 20:32:53.470: E/AndroidRuntime(796): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1870) 
    09-30 20:32:53.470: E/AndroidRuntime(796): at android.os.Handler.dispatchMessage(Handler.java:99) 
    09-30 20:32:53.470: E/AndroidRuntime(796): at android.os.Looper.loop(Looper.java:123) 
    09-30 20:32:53.470: E/AndroidRuntime(796): at android.app.ActivityThread.main(ActivityThread.java:4363) 
    09-30 20:32:53.470: E/AndroidRuntime(796): at java.lang.reflect.Method.invokeNative(Native Method) 
    09-30 20:32:53.470: E/AndroidRuntime(796): at java.lang.reflect.Method.invoke(Method.java:521) 
    09-30 20:32:53.470: E/AndroidRuntime(796): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860) 
    09-30 20:32:53.470: E/AndroidRuntime(796): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) 
    09-30 20:32:53.470: E/AndroidRuntime(796): at dalvik.system.NativeStart.main(Native Method) 
    09-30 20:32:53.470: E/AndroidRuntime(796): Caused by: java.lang.IllegalStateException: Failure saving state: active BookDetailFragment{43e7e0d8} has cleared index: -1 
    09-30 20:32:53.470: E/AndroidRuntime(796): at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1716) 
    09-30 20:32:53.470: E/AndroidRuntime(796): at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:532) 
    09-30 20:32:53.470: E/AndroidRuntime(796): at com.actionbarsherlock.app.SherlockFragmentActivity.onSaveInstanceState(SherlockFragmentActivity.java:126) 
    09-30 20:32:53.470: E/AndroidRuntime(796): at com.jeremyfeinstein.slidingmenu.lib.app.SlidingFragmentActivity.onSaveInstanceState(SlidingFragmentActivity.java:51) 
    09-30 20:32:53.470: E/AndroidRuntime(796): at com.org.scgroup.MainActivity.onSaveInstanceState(MainActivity.java:324) 
    09-30 20:32:53.470: E/AndroidRuntime(796): at android.app.Activity.performSaveInstanceState(Activity.java:1022) 
    09-30 20:32:53.470: E/AndroidRuntime(796): at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1180) 
    09-30 20:32:53.470: E/AndroidRuntime(796): at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3144) 
    09-30 20:32:53.470: E/AndroidRuntime(796): ... 12 more 

onSaveInstanceState實現方法具d:

@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    try { 
     Ln.d("onSaveInstanceState"); 
     outState.putInt(CURRENT_FRAGMENT_KEY, currentFragment); 

     if (0 < getSupportFragmentManager().getBackStackEntryCount()) { 
      getSupportFragmentManager().putFragment(outState, "mContent", mContent); 
     } 
    } catch (Exception ex) { 
     Ln.e(ex); 
    } 
} 

你能告訴我我在哪裏錯了嗎?提前致謝。

+0

你找到這個問題的任何解決方案? –

+1

不,我只是搞清楚,當我使用替代而不是分離和附加片段,這個錯誤沒有發生。 – R4j

+0

我不認爲這是解決方案,在我的情況下,我做了一個替換交易完成這個錯誤太:/ – letroll

回答

0

試圖改變

@Override 
public void onBackPressed() { 
    if (0 == getSupportFragmentManager().getBackStackEntryCount()) { 

@Override 
public void onBackPressed() { 
    if (getSupportFragmentManager().getBackStackEntryCount() < 1) { 

我希望這個作品

+0

不能修復我的錯誤... – Virthuss

0

您的片段的狀態會自動保存和恢復,沒有必要在做任何事情你onSaveInstanceState方法。不要在您的活動中持有任何對您的片段的引用(在您的案例中currentFragment,mContent),如果您需要某個片段,請通過例如FragmentManager獲取它。 findFragmentByTag

0

分離和附着之間讀取碎片狀態可能存在問題。

嘗試使用這樣的:

FragmentTransaction ft = fragmentManager.beginTransaction(); 

//ft.detach(fragment).attach(fragment).commitAllowingStateLoss(); // crashing 

ft.replace(getView().getId(), fragment).commitAllowingStateLoss(); // not crashing 
相關問題