无法保存RelativeLayout的状态

时间:2016-03-09 12:01:32

标签: java android parcelable serializable android-savedstate

我正在创建一个具有活动的应用程序,用户可以在其中添加图片到对象,这里我给用户选择从图库中选择图片或使用相机创建新图片。这两个选项都启动一个Intent(在用户完成选择图片之后)返回它来自的活动。

但我遇到的问题是,当用户回来时,有时某些活动的数据会丢失,所以我使用savedInstance“重新加载”它。我尝试保存的对象之一是自定义视图的arraylist,它扩展了RelativeLayout。但是当我加载以下数据时,我收到以下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sition.projectmanager/com.example.sition.projectmanager.activities.ObjectActivity}: java.lang.RuntimeException: Parcelable encountered IOException reading a Serializable object (name = com.example.sition.projectmanager.TabButton)
           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
           at android.app.ActivityThread.access$700(ActivityThread.java:158)
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296)
           at android.os.Handler.dispatchMessage(Handler.java:99)
           at android.os.Looper.loop(Looper.java:176)
           at android.app.ActivityThread.main(ActivityThread.java:5365)
           at java.lang.reflect.Method.invokeNative(Native Method)
           at java.lang.reflect.Method.invoke(Method.java:511)
           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
           at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.RuntimeException: Parcelable encountered IOException reading a Serializable object (name = com.example.sition.projectmanager.TabButton)
           at android.os.Parcel.readSerializable(Parcel.java:2144)
           at android.os.Parcel.readValue(Parcel.java:2016)
           at android.os.Parcel.readListInternal(Parcel.java:2235)
           at android.os.Parcel.readArrayList(Parcel.java:1655)
           at android.os.Parcel.readValue(Parcel.java:1986)
           at android.os.Parcel.readMapInternal(Parcel.java:2226)
           at android.os.Bundle.unparcel(Bundle.java:223)
           at android.os.Bundle.getParcelable(Bundle.java:1173)
           at android.app.Activity.onCreate(Activity.java:930)
           at com.example.sition.projectmanager.activities.ObjectActivity.onCreate(ObjectActivity.java:102)
           at android.app.Activity.performCreate(Activity.java:5326)
           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225)
           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316) 
           at android.app.ActivityThread.access$700(ActivityThread.java:158) 
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296) 
           at android.os.Handler.dispatchMessage(Handler.java:99) 
           at android.os.Looper.loop(Looper.java:176) 
           at android.app.ActivityThread.main(ActivityThread.java:5365) 
           at java.lang.reflect.Method.invokeNative(Native Method) 
           at java.lang.reflect.Method.invoke(Method.java:511) 
           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
           at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.io.InvalidClassException: android.widget.RelativeLayout; IllegalAccessException
           at java.io.ObjectStreamClass.resolveConstructorClass(ObjectStreamClass.java:692)
           at java.io.ObjectStreamClass.newInstance(ObjectStreamClass.java:653)
           at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1826)
           at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787)
           at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2013)
           at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1970)
           at android.os.Parcel.readSerializable(Parcel.java:2142)
           at android.os.Parcel.readValue(Parcel.java:2016) 
           at android.os.Parcel.readListInternal(Parcel.java:2235) 
           at android.os.Parcel.readArrayList(Parcel.java:1655) 
           at android.os.Parcel.readValue(Parcel.java:1986) 
           at android.os.Parcel.readMapInternal(Parcel.java:2226) 
           at android.os.Bundle.unparcel(Bundle.java:223) 
           at android.os.Bundle.getParcelable(Bundle.java:1173) 
           at android.app.Activity.onCreate(Activity.java:930) 
           at com.example.sition.projectmanager.activities.ObjectActivity.onCreate(ObjectActivity.java:102) 
           at android.app.Activity.performCreate(Activity.java:5326) 
           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097) 
           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2225) 
           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316) 
           at android.app.ActivityThread.access$700(ActivityThread.java:158) 
           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1296) 
           at android.os.Handler.dispatchMessage(Handler.java:99) 
           at android.os.Looper.loop(Looper.java:176) 
           at android.app.ActivityThread.main(ActivityThread.java:5365) 
           at java.lang.reflect.Method.invokeNative(Native Method) 
           at java.lang.reflect.Method.invoke(Method.java:511) 
           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
           at dalvik.system.NativeStart.main(Native Method) 

这是我的自定义RelativeLayout类:

public class TabButton extends RelativeLayout implements Serializable {
    public TabButton(Context context) {
         super(context);
    }

    public TabButton(Context context, AttributeSet attrs) {
         super(context, attrs);
    }

    public TabButton(Context context, AttributeSet attrs, int defStyleAttr) {
         super(context, attrs, defStyleAttr);
    }
}

我的onSaveInstanceState和onRestoreInstanceState:

@Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putSerializable("CURRENT_OBJECT",currentObject);
    outState.putSerializable("TAB_BUTTONS", tabButtons);
    super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
    currentObject = (Objectable) savedInstanceState.getSerializable("CURRENT_OBJECT");
    tabButtons = (ArrayList<TabButton>) savedInstanceState.getSerializable("TAB_BUTTONS");
}

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您的概念是错误的:您不应该保存整个小部件,而应该将信息保存到重新创建

  

但是,您的活动可能包含更多您所掌握的状态信息   喜欢恢复,例如跟踪用户的成员变量   活动进展。

来自:Android Activity Lifecycle, Android SDK Docs

因此,将信息保存为整数(在您的示例中用于标识所选标签)或其他基元,并在onRestoreInstanceState中设置。