获取Parcelable遇到IOException编写可序列化对象

时间:2019-02-28 06:09:19

标签: java android parcelable ioexception serializable

尝试传递自定义对象时,我遇到Parcelable encountered IOException writing serializable object异常。以下是我的代码段:

val intent = Intent(this@MyDowoodleActivity, CreateDowoodleActivity::class.java)
intent.putExtra("DATA", Activity())
startActivity(intent)

模型类:

 data class Activity(
    @SerializedName("isDeleted")
    val isDeleted: Int = 0,
    @SerializedName("startDate")
    **val startDate: Any = Any()** // I think this exception is because of Any type

 ):Serializable

但是我想将Any()作为类型。该怎么做?

日志显示以下内容:

    java.lang.RuntimeException: Parcelable encountered IOException writing 
   serializable object (name = com.smartlayer.dowoodle.Activity)
    at android.os.Parcel.writeSerializable(Parcel.java:1536)
    at android.os.Parcel.writeValue(Parcel.java:1484)
    at android.os.Parcel.writeArrayMapInternal(Parcel.java:733)
    at android.os.BaseBundle.writeToParcelInner(BaseBundle.java:1408)
    at android.os.Bundle.writeToParcel(Bundle.java:1133)
    at android.os.Parcel.writeBundle(Parcel.java:773)
    at android.content.Intent.writeToParcel(Intent.java:9253)
    at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3545)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1526)
    at android.app.Activity.startActivityForResult(Activity.java:4404)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:767)
    at android.app.Activity.startActivityForResult(Activity.java:4363)
    at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:754)
    at android.app.Activity.startActivity(Activity.java:4687)
    at android.app.Activity.startActivity(Activity.java:4655)
    at com.smartlayer.dowoodle.create_dowoodle.MyDowoodleActivity$onDowoodleEdit$1.onResponse(MyDowoodleActivity.kt:66)
    at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:70)
    at android.os.Handler.handleCallback(Handler.java:751)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:6776)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1496)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1386)
 Caused by: java.io.NotSerializableException: java.lang.Object
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1224)
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1584)
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1549)
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1472)
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1218)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:346)

任何答案将不胜感激。 谢谢

1 个答案:

答案 0 :(得分:0)

是的,没错。

https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html

Any()对象未标记为可序列化,如果您将startdate转换为Date或DateTime甚至是long,并添加了将longdate转换为startdate的方法,就可以了。