获取异常作为失败保存状态:当我按下Android设备的主页按钮时,活动片段已清除索引:-1

时间:2014-05-13 12:39:16

标签: android android-fragments android-fragmentactivity

当我按下Android的主页按钮并开始新活动时,我正面临一些奇怪的问题。

我的堆栈跟踪如下

05-13 18:06:21.182: E/FragmentManager(5674): Failure saving state: active Fragment{411d96d8} has cleared index: -1
05-13 18:06:21.182: E/FragmentManager(5674): Activity state:
05-13 18:06:22.032: E/ACRA(5674): ACRA caught a IllegalStateException exception for com.itgurussoftware.android.dineback. Building report.
05-13 18:06:27.732: E/ACRA(5674): com.itgurussoftware.android.dineback fatal error : Failure saving state: active Fragment{411d96d8} has cleared index: -1
05-13 18:06:27.732: E/ACRA(5674): java.lang.IllegalStateException: Failure saving state: active Fragment{411d96d8} has cleared index: -1
05-13 18:06:27.732: E/ACRA(5674):   at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1723)
05-13 18:06:27.732: E/ACRA(5674):   at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:546)
05-13 18:06:27.732: E/ACRA(5674):   at android.app.Activity.performSaveInstanceState(Activity.java:1113)
05-13 18:06:27.732: E/ACRA(5674):   at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1185)
05-13 18:06:27.732: E/ACRA(5674):   at android.app.ActivityThread.performStopActivityInner(ActivityThread.java:2793)
05-13 18:06:27.732: E/ACRA(5674):   at android.app.ActivityThread.handleStopActivity(ActivityThread.java:2851)
05-13 18:06:27.732: E/ACRA(5674):   at android.app.ActivityThread.access$900(ActivityThread.java:123)
05-13 18:06:27.732: E/ACRA(5674):   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1164)
05-13 18:06:27.732: E/ACRA(5674):   at android.os.Handler.dispatchMessage(Handler.java:99)
05-13 18:06:27.732: E/ACRA(5674):   at android.os.Looper.loop(Looper.java:137)
05-13 18:06:27.732: E/ACRA(5674):   at android.app.ActivityThread.main(ActivityThread.java:4424)
05-13 18:06:27.732: E/ACRA(5674):   at java.lang.reflect.Method.invokeNative(Native Method)
05-13 18:06:27.732: E/ACRA(5674):   at java.lang.reflect.Method.invoke(Method.java:511)
05-13 18:06:27.732: E/ACRA(5674):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-13 18:06:27.732: E/ACRA(5674):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-13 18:06:27.732: E/ACRA(5674):   at dalvik.system.NativeStart.main(Native Method)

我添加和删除片段的代码如下

        FragmentContainer fragContainer = container.get(container.size() - 1);
        Fragment fragment = fragContainer.getFragment();
        String tag = fragContainer.getTabName();
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction ft = manager.beginTransaction();

        mTabHost.setCurrentTabByTag(tag);

        if (!fragment.isAdded()) {

            ft.remove(fragment);
        }
        if(container.size()>0)
            container.remove(container.size() - 1);
        ft.replace(R.id.realtabcontent, fragment);
        ft.commitAllowingStateLoss();

尝试

  • 更新支持库
  • 尝试将onSaveInstanceState重写为

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        if (outState != null) {
            super.onSaveInstanceState(outState);
        }
    }
    

2 个答案:

答案 0 :(得分:2)

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState, this, TAG_TAFFER_TIME);
    mFragmentHolder = (RelativeLayout) findViewById(R.id.fragment);
    mFragmentTransaction = getSupportFragmentManager().beginTransaction();
    mFragmentTransaction.add(mFragmentHolder.getId(), mFragmentTafferTime, TAG);
    mFragmentTransaction.addToBackStack(TAG);
    mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    mFragmentTransaction.commit();
  } 

// On fragment change try to use this function
private void changeFragment(SherlockFragment fragment, String tag) {
    try {
        mFragmentTransaction.replace(mFragmentHolder.getId(), fragment, tag);
        mFragmentTransaction.addToBackStack(tag);
        mFragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        mFragmentTransaction.commit();
    } catch (Exception exception) {

    }
}

答案 1 :(得分:0)

就我而言,我做到了。

        If row.Cells(1).FormattedValue = My.Settings.PEST And Format(DateInterval.Month, "dd" = "02") Then
            Month2 += row.Cells(2).Value
        End If
    Next
    lblFeb.Text = Month2
    DataGridView1.Rows.Add({Month2})

我也将 @Override public void onDestroyView() { super.onDestroyView(); if (mActivity.getSupportFragmentManager() != null && mAlbumFragment != null && mAlbumFragment.isAdded()) { FragmentTransaction trans = mActivity.getSupportFragmentManager().beginTransaction(); trans.remove(mAlbumFragment).commitAllowingStateLoss(); } } 替换为FragmentTransaction.add()。 如果使用单个片段,则可以尝试此操作。我认为这样做就可以解决问题。