onSaveInstanceState方法

时间:2015-10-29 08:55:15

标签: android android-fragments android-savedstate

对于其中一个用户(motorola motorola XT1068),我的onSaveInstanceState异常。我正在使用ACRA进行崩溃报告。 这是完整的堆栈跟踪:

java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1438)
at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1456)
at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:634)
at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:613)
at android.support.v4.app.FragmentTabHost.onAttachedToWindow(FragmentTabHost.java:282)
at android.view.View.dispatchAttachedToWindow(View.java:13424)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2709)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2716)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2716)
at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2716)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1315)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1077)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5884)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5312)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)

但是我的应用程序源没有提到,这是我使用saveinstance方法的唯一地方。

@Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putBundle("CONFIG", (savedState != null) ? savedState : saveState());
    }
private Bundle saveState() { 
        Bundle state = new Bundle();
        state.putCharSequence("SUCCESS", result.getText());
       state.putCharSequence("FAILURE", error.getText());
        return state;
    }

那么这可能是什么问题呢?

修改

这是ViewPagerAdapter代码

public class ViewPagerAdapter extends FragmentPagerAdapter {

    private final int PAGES = 3;
    private FragmentManager mFragmentManager;
    private Context mContext;
    private String[] title = new String[]{"Frag1",
            "Frag2", "Frag3"};

    public ViewPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        mContext = context;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                return new Fragment1();
            case 1:
                return new Fragment2();
            case 2:
                return new Fragment3();
            default:
                throw new IllegalArgumentException(
                        "The item position should be less or equal to:" + PAGES);
        }
    }

    @Override
    public int getCount() {
        return PAGES;
    }

    @Override
    public int getItemPosition(Object object) {
        return POSITION_NONE;
    }

   @Override
    public CharSequence getPageTitle(int position) {
        return title[position];
    }
}

从活动中调用

viewPageAdapter = new ViewPagerAdapter(getSupportFragmentManager(), getApplicationContext());

1 个答案:

答案 0 :(得分:-2)

最后调用super方法。

@Override
public void onSaveInstanceState(Bundle outState) {
    outState.putBundle("CONFIG", (savedState != null) ? savedState : saveState());
    super.onSaveInstanceState(outState);
}