Android - 可序列化接口的问题
我一直在使用 Serialized
接口将对象从一个活动传递到另一个活动。我在发送方使用 putExtra
,在接收方使用 getSerialized
。一切正常,但我(第一次)收到以下错误报告:
java.lang.RuntimeException:Parcelable 读取时遇到 IOException 可序列化对象
我不明白为什么会生成此异常,因为我使用的是 getSerialized
而不是 getParcelable
我知道我应该实现 Parcelable
接口,因为它是专门为 Android 设计的(这就是我最终要做的),但我想了解为什么会出现此错误。
谢谢!
I have been using the Serializable
interface to pass an object from one activity to another. I am using putExtra
on the sender side and getSerializable
on the receiver side. Everything works fine but I have received (for the first time) the following error report:
java.lang.RuntimeException: Parcelable encountered IOException reading
a Serializable object
I don't understand why this exception has been generated since I am using getSerializable
and not getParcelable
.
I know that I should implement the Parcelable
interface instead because it has been designed specifically for Android (and that's what I will end up doing) but I want to understand why I am getting this error.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
此错误中提到了
Parcelable
,因为您从一个Activity
发送到另一个Activity
的Intent
内部有一个Bundle
,并且此Bundle
是可打包
。当您调用Intent.putExtra()
时,此额外内容将添加到内部Bundle
中。当Intent
在活动之间传递时,其Bundle
会与字节数组相互转换,您的Serialized
对象也是如此。但我不知道为什么会出现这个错误。也许是因为
writeObject()
/readObject()
实现中存在一些错误。Parcelable
is mentioned in this error because anIntent
you send from oneActivity
to another has aBundle
inside and thisBundle
isParcelable
. When you callIntent.putExtra()
this extra is added to the innerBundle
. WhenIntent
is passed between activities itsBundle
is converted to and from a byte array and so is yourSerializable
object.But I don't know why this error occurs. Maybe it's because of some bug in
writeObject()
/readObject()
implementation.