失败保存活动状态,扩展另一个活动

时间:2014-01-08 00:14:41

标签: android

我的问题如下。 我有BasicPhotoTakingActivity扩展SherlockFragmentActivity,它具有拍摄照片的所有方法,如takePhoto()loadFromGallery()等。

现在我有另一个扩展BasicPhotoTakingActivity的活动。

出于某种原因,应用程序正在崩溃onSaveInstanceState,例如在旋转设备屏幕时。

以下是代码:

ActivityWhichExtendsBasicPhotoTakingActivity onSaveInstanceState:

@Override
protected void onSaveInstanceState(Bundle outState) {

    outState.putInt("state", currentStage.ordinal());

    super.onSaveInstanceState(outState);
}

BasicPhotoTakingActivity onSaveInstanceState:

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putString("imageUri", mCurrentPhotoPath);
    if (bitmap != null) {
        Logger.d(TAG, "Bitmap != null. Saving: " + bitmap.toString());
        outState.putBoolean("bitmap", true);
    }
    super.onSaveInstanceState(outState);
}

崩溃日志:

java.lang.IllegalStateException: Failure saving state: active AddBoatAddressFragment$1{65781b60} has cleared index: -1
        at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1660)
        at android.support.v4.app.Fragment.performSaveInstanceState(Fragment.java:1627)
        at android.support.v4.app.FragmentManagerImpl.saveFragmentBasicState(FragmentManager.java:1603)
        at android.support.v4.app.FragmentManagerImpl.saveAllState(FragmentManager.java:1671)
        at android.support.v4.app.FragmentActivity.onSaveInstanceState(FragmentActivity.java:532)
        at com.actionbarsherlock.app.SherlockFragmentActivity.onSaveInstanceState(SherlockFragmentActivity.java:127)
        at com.android.activities.BasicPhotoTakingActivity.onSaveInstanceState(BasicPhotoTakingActivity.java:86)
        at com.android.activities.AddMyBoatActivity.onSaveInstanceState(AddMyBoatActivity.java:97)
        at android.app.Activity.performSaveInstanceState(Activity.java:1152)
        at android.app.Instrumentation.callActivityOnSaveInstanceState(Instrumentation.java:1223)
        at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3693)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1403)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4998)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)

2 个答案:

答案 0 :(得分:1)

试试这个,

@Override
protected void onSaveInstanceState(Bundle outState) {
    if (outState!= null && outState.size() > 0) {

}

答案 1 :(得分:0)

如果不知道你的所有代码(例如你的活动在你的清单中如何宣告,哪些碎片是没有附加和活动的等等),那么肯定很难说。我最初的想法是你(有意或无意)保留AddBoatAddressFragment但告诉FragmentManager保留它的实例,因此Fragment在Activity被设置为被销毁时(即从旋转)仍然是活动的。我无法肯定地说,但这是来自FragmentManager类,并解释了堆栈跟踪中的第一行:

Parcelable saveAllState() {
    // Make sure all pending operations have now been executed to get
    // our state update-to-date.
    execPendingActions();

    mStateSaved = true;

    if (mActive == null || mActive.size() <= 0) {
        return null;
    }

    // First collect all active fragments.
    int N = mActive.size();
    FragmentState[] active = new FragmentState[N];
    boolean haveFragments = false;
    for (int i=0; i<N; i++) {
        Fragment f = mActive.get(i);
        if (f != null) {
            if (f.mIndex < 0) {
                throwException(new IllegalStateException(
                        "Failure saving state: active " + f
                        + " has cleared index: " + f.mIndex));
            }

            haveFragments = true;

            FragmentState fs = new FragmentState(f);
            active[i] = fs;

            if (f.mState > Fragment.INITIALIZING && fs.mSavedFragmentState == null) {
                fs.mSavedFragmentState = saveFragmentBasicState(f);

                if (f.mTarget != null) {
                    if (f.mTarget.mIndex < 0) {
                        throwException(new IllegalStateException(
                                "Failure saving state: " + f
                                + " has target not in fragment manager: " + f.mTarget));
                    }
                    if (fs.mSavedFragmentState == null) {
                        fs.mSavedFragmentState = new Bundle();
                    }
                    putFragment(fs.mSavedFragmentState,
                            FragmentManagerImpl.TARGET_STATE_TAG, f.mTarget);
                    if (f.mTargetRequestCode != 0) {
                        fs.mSavedFragmentState.putInt(
                                FragmentManagerImpl.TARGET_REQUEST_CODE_STATE_TAG,
                                f.mTargetRequestCode);
                    }
                }

            } else {
                fs.mSavedFragmentState = f.mSavedFragmentState;
            }

            if (DEBUG) Log.v(TAG, "Saved state of " + f + ": "
                    + fs.mSavedFragmentState);
        }
    }