Parcelable遇到IOException,编写可序列化对象SparseArray <int []>

时间:2016-07-18 19:33:13

标签: java android serialization sparse-matrix

所以我想在Bundle中发送一个SparseArray,但是因为我不能创建一个类来使SparseArray成为一个Serializable对象,但现在当我运行我的应用程序时它会产生这个错误输出。

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.awemobi.legominifigures.DataHelper)

提出这个问题的课程是下一个:

public class DataHelper implements Serializable {
private SparseArray<int[]> data;

public DataHelper(SparseArray<int[]> data)
{
    this.data = data;
}

public SparseArray<int[]> getArray()
{
    return  data;
}

}

我认为可能是因为int []而且我甚至创建了另一个类Positions来使int []可序列化但问题仍然存在。我还可以尝试做些什么?我无法准确追踪错误的原因。

这是完整的输出:

FATAL EXCEPTION: main
Process: com.awemobi.legominifigures, PID: 18077
java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.awemobi.legominifigures.DataHelper)
    at android.os.Parcel.writeSerializable(Parcel.java:1388)
    at android.os.Parcel.writeValue(Parcel.java:1335)
    at android.os.Parcel.writeArrayMapInternal(Parcel.java:638)
    at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)
    at android.os.Bundle.writeToParcel(Bundle.java:1096)
    at android.os.Parcel.writeBundle(Parcel.java:663)
    at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:137)
    at android.os.Parcel.writeTypedArray(Parcel.java:1191)
    at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:384)
    at android.os.Parcel.writeParcelable(Parcel.java:1357)
    at android.os.Parcel.writeValue(Parcel.java:1262)
    at android.os.Parcel.writeArrayMapInternal(Parcel.java:638)
    at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313)
    at android.os.Bundle.writeToParcel(Bundle.java:1096)
    at android.os.Parcel.writeBundle(Parcel.java:663)
    at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2944)
    at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3355)
    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:5376)
    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:908)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
Caused by: java.io.NotSerializableException: android.util.SparseArray
    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1344)
    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
    at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:959)
    at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:360)
    at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1054)
    at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1384)
    at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1651)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1497)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1461)
    at android.os.Parcel.writeSerializable(Parcel.java:1383)
    at android.os.Parcel.writeValue(Parcel.java:1335) 
    at android.os.Parcel.writeArrayMapInternal(Parcel.java:638) 
    at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313) 
    at android.os.Bundle.writeToParcel(Bundle.java:1096) 
    at android.os.Parcel.writeBundle(Parcel.java:663) 
    at android.support.v4.app.FragmentState.writeToParcel(Fragment.java:137) 
    at android.os.Parcel.writeTypedArray(Parcel.java:1191) 
    at android.support.v4.app.FragmentManagerState.writeToParcel(FragmentManager.java:384) 
    at android.os.Parcel.writeParcelable(Parcel.java:1357) 
    at android.os.Parcel.writeValue(Parcel.java:1262) 
    at android.os.Parcel.writeArrayMapInternal(Parcel.java:638) 
    at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1313) 
    at android.os.Bundle.writeToParcel(Bundle.java:1096) 
    at android.os.Parcel.writeBundle(Parcel.java:663) 
    at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:2944) 
    at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3355) 
    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:5376) 
    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:908) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703) 

1 个答案:

答案 0 :(得分:0)

尝试将SparseArray变量“data”声明为瞬态,如下所示,

private transient SparseArray<int[]> data;