获取 Parcelable 遇到 IOException 写入可序列化对象
Posted
技术标签:
【中文标题】获取 Parcelable 遇到 IOException 写入可序列化对象【英文标题】:Getting Parcelable encountered IOException writing serializable object 【发布时间】:2019-07-21 23:58:31 【问题描述】:我在尝试传递自定义对象时收到Parcelable encountered IOException writing serializable object
异常。下面是我的代码sn-p:
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】:是的,你是对的。
https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/-any/index.html
Any() 对象未标记为可序列化,如果您将 startdate 转换为 Date 或 DateTime 甚至 long 并添加将 long 转换为 startdate 的方法,您应该没问题。
【讨论】:
以上是关于获取 Parcelable 遇到 IOException 写入可序列化对象的主要内容,如果未能解决你的问题,请参考以下文章
Parcelable 遇到ClassNotFoundException 读取一个Serializable 对象
无法解决“Parcelable 遇到写入可序列化对象的 IOException”
Parcelable 在写入可序列化对象时遇到 IOException
Parcelable 遇到 IOException 写入可序列化对象 getactivity()