我的问题如下。
我有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)
最佳答案
尝试这个,
@Override
protected void onSaveInstanceState(Bundle outState) {
if (outState!= null && outState.size() > 0) {
}