java.lang.RuntimeException: Parcelable 遇到 IOException 写入可序列化对象 (name = io.realm.StationAndLocationMo

Posted

技术标签:

【中文标题】java.lang.RuntimeException: Parcelable 遇到 IOException 写入可序列化对象 (name = io.realm.StationAndLocationModelRealmProxy)【英文标题】:java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = io.realm.StationAndLocationModelRealmProxy) 【发布时间】:2021-12-04 07:58:52 【问题描述】:

realm 数据库使用 findfirst() 方法将数据发送到 mBundle.putSerializable("data", (Serializable) item);项目数据值能够接收下一个活动它的显示

【问题讨论】:

【参考方案1】:

您需要使您的对象可打包,而不是可序列化。

public class Channel extends RealmObject implements Parcelable 

    private String channelName;
    private String channelUrl;
    private String channelImg;
    private String channelGroup;

    public Channel() 
    

    public String getChannelName() 
        return channelName;
    

    public void setChannelName(String channelName) 
        this.channelName = channelName;
    

    public String getChannelUrl() 
        return channelUrl;
    

    public void setChannelUrl(String channelUrl) 
        this.channelUrl = channelUrl;
    

    public String getChannelImg() 
        return channelImg;
    

    public void setChannelImg(String channelImg) 
        this.channelImg = channelImg;
    

    public String getChannelGroup() 
        return channelGroup;
    

    public void setChannelGroup(String channelGroup) 
        this.channelGroup = channelGroup;
    

    @NonNull
    @Override
    public String toString() 
        return "Channel" +
                ", channelName='" + channelName + '\'' +
                ", channelUrl='" + channelUrl + '\'' +
                ", channelImg='" + channelImg + '\'' +
                ", channelGroup='" + channelGroup + '\'' +
                '';
    

    public Channel(Parcel in) 
        String[] data = new String[4];
        in.readStringArray(data);
        this.channelName = data[0];
        this.channelUrl = data[1];
        this.channelImg = data[2];
        this.channelGroup = data[3];
    

    @Override
    public int describeContents() 
        return 0;
    

    @Override
    public void writeToParcel(Parcel parcel, int i) 
        parcel.writeStringArray(new String[]this.channelName, this.channelUrl, this.channelImg, this.channelGroup);
    

    public static final Parcelable.Creator CREATOR = new Parcelable.Creator<Channel>() 
        public Channel createFromParcel(Parcel in) 
            return new Channel(in);
        

        @Override
        public Channel[] newArray(int i) 
            return new Channel[i];
        
    ;

【讨论】:

这里使用 putSerializable 方法发送 Bundle 数据 - mBundle.putSerializable("data", (Serializable) item); 意图无法将数据传递给下一个活动显示logcat 标题警告 是的,它给出这个是正常的,因为领域对象需要是可打包的,而不是可序列化的。 Parcelable 首先使领域对象类像上面那样可打包,然后您可以在有意移动对象的同时执行此链接中的操作。

以上是关于java.lang.RuntimeException: Parcelable 遇到 IOException 写入可序列化对象 (name = io.realm.StationAndLocationMo的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio 按钮导致崩溃

添加 ImageView 时应用程序崩溃?