我前一天遇到了这个问题。我有一个ListActivity,在旋转屏幕时我必须保留某些对象,对象是:
private ArrayList<CategoryToTest> _categories;
private ArrayList<CategoryAnalisisWS> _categoriesAnalisis;
private int _categoryToTestIndex = 0;
private int _myVehicleId;
private String _token;
所有这些对象都实现了Serializable。 所以我创建了一个自定义对象,我保存在onSaveInstanceState,
private class MyBundleObject implements java.io.Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
ArrayList<CategoryAnalisisWS> _categoriesAnalisis;
ArrayList<CategoryToTest> _categories;
int _categoryToTestIndex;
int _myVehicleId;
String _token;
}
在onSaveInstanceState中,
@Override
protected void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
MyBundleObject object = new MyBundleObject();
object._categories = _categories;
object._categoriesAnalisis = _categoriesAnalisis;
object._categoryToTestIndex = _categoryToTestIndex;
object._myVehicleId = _myVehicleId;
object._token = _token;
outState.putSerializable("change", object);
}
然后在onCreate方法中:
if (savedInstanceState == null)
getCategoriesToTestFromServer();
else
{
MyBundleObject object = (MyBundleObject)savedInstanceState.getSerializable("change");
_categories = object._categories;
_categoriesAnalisis = object._categoriesAnalisis;
_categoryToTestIndex =object. _categoryToTestIndex;
_myVehicleId =object. _myVehicleId;
_token = object._token;
}
getCategoriesToTestFromServer()调用asyncTask。当屏幕旋转发生时,一切正常,问题是当屏幕关闭时(自动或按下电源按钮),此时我得到一个Runtimeexception,编写可序列化对象,由NotSerializableException引起:CategoriesToTestActivity,这里日志:
06-15 10:12:33.730:E / AndroidRuntime(16141):致命异常:主要 06-15 10:12:33.730:E / AndroidRuntime(16141):java.lang.RuntimeException:Parcelable遇到IOException编写可序列化对象(name = com.decautomotive.sslite.CategoriesToTestActivity $ MyBundleObject) 06-15 10:12:33.730:E / AndroidRuntime(16141):在android.os.Parcel.writeSerializable(Parcel.java:1175) 06-15 10:12:33.730:E / AndroidRuntime(16141):在android.os.Parcel.writeValue(Parcel.java:1129) 06-15 10:12:33.730:E / AndroidRuntime(16141):在android.os.Parcel.writeMapInternal(Parcel.java:487) 06-15 10:12:33.730:E / AndroidRuntime(16141):在android.os.Bundle.writeToParcel(Bundle.java:1602) 06-15 10:12:33.730:E / AndroidRuntime(16141):在android.os.Parcel.writeBundle(Parcel.java:501) 06-15 10:12:33.730:E / AndroidRuntime(16141):在android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:1791) 06-15 10:12:33.730:E / AndroidRuntime(16141):在android.app.ActivityThread.handleStopActivity(ActivityThread.java:2704) 06-15 10:12:33.730:E / AndroidRuntime(16141):在android.app.ActivityThread.access $ 800(ActivityThread.java:122) 06-15 10:12:33.730:E / AndroidRuntime(16141):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1038) 06-15 10:12:33.730:E / AndroidRuntime(16141):在android.os.Handler.dispatchMessage(Handler.java:99) 06-15 10:12:33.730:E / AndroidRuntime(16141):在android.os.Looper.loop(Looper.java:132) 06-15 10:12:33.730:E / AndroidRuntime(16141):在android.app.ActivityThread.main(ActivityThread.java:4123) 06-15 10:12:33.730:E / AndroidRuntime(16141):at java.lang.reflect.Method.invokeNative(Native Method) 06-15 10:12:33.730:E / AndroidRuntime(16141):at java.lang.reflect.Method.invoke(Method.java:491) 06-15 10:12:33.730:E / AndroidRuntime(16141):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:841) 06-15 10:12:33.730:E / AndroidRuntime(16141):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599) 06-15 10:12:33.730:E / AndroidRuntime(16141):at dalvik.system.NativeStart.main(Native Method) 06-15 10:12:33.730:E / AndroidRuntime(16141):引起:java.io.NotSerializableException:com.pepe.base.CategoriesToTestActivity 06-15 10:12:33.730:E / AndroidRuntime(16141):at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1435) 06-15 10:12:33.730:E / AndroidRuntime(16141):at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1742) 06-15 10:12:33.730:E / AndroidRuntime(16141):at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1588) 06-15 10:12:33.730:E / AndroidRuntime(16141):at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1552) 06-15 10:12:33.730:E / AndroidRuntime(16141):at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1050) 06-15 10:12:33.730:E / AndroidRuntime(16141):at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:411) 06-15 10:12:33.730:E / AndroidRuntime(16141):at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1145) 06-15 10:12:33.730:E / AndroidRuntime(16141):at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1475) 06-15 10:12:33.730:E / AndroidRuntime(16141):at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1742) 06-15 10:12:33.730:E / AndroidRuntime(16141):at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1588) 06-15 10:12:33.730:E / AndroidRuntime(16141):at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1552) 06-15 10:12:33.730:E / AndroidRuntime(16141):at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:1050) 06-15 10:12:33.730:E / AndroidRuntime(16141):at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:411) 06-15 10:12:33.730:E / AndroidRuntime(16141):at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1145) 06-15 10:12:33.730:E / AndroidRuntime(16141):at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1475) 06-15 10:12:33.730:E / AndroidRuntime(16141):at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1742) 06-15 10:12:33.730:E / AndroidRuntime(16141):at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1588) 06-15 10:12:33.730:E / AndroidRuntime(16141):at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1552) 06-15 10:12:33.730:E / AndroidRuntime(16141):在android.os.Parcel.writeSerializable(Parcel.java:1170)
似乎正在尝试序列化活动,我不明白,而且疯狂的是,当屏幕旋转时,所有事情都顺利进行,只有在屏幕转动时的。 请,任何想法?,或者可能是另一个保存这些实例对象的建议?