栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

未经检查的将java.io.Serializable强制转换为java.util.ArrayList

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

未经检查的将java.io.Serializable强制转换为java.util.ArrayList

根本原因:
这是来自IDE的警告,

getSerializableExtra
返回
Serializable
,并且您尝试转换为
ArrayList<PuntoNota>
。如果程序无法将其强制转换为期望的类型,则可能在运行时引发
ClassCastException

解决方案: 在android中传递用户定义的对象时,您的类应实现

Parcelable
而不是
Serializable
接口。

class Puntonota implements Parcelable {    private String punto;    private String nota;    public Puntonota(String punto, String nota) {        this.punto = punto;        this.nota = nota;    }    protected Puntonota(Parcel in) {        punto = in.readString();        nota = in.readString();    }    public String getPunto() {        return punto;    }    public String getNota() {        return nota;    }    @Override    public int describeContents() {        return 0;    }    @Override    public void writeToParcel(Parcel dest, int flags) {        dest.writeString(punto);        dest.writeString(nota);    }    public static final Creator<PuntoNota> CREATOR = new Creator<PuntoNota>() {        @Override        public Puntonota createFromParcel(Parcel in) { return new Puntonota(in);        }        @Override        public PuntoNota[] newArray(int size) { return new PuntoNota[size];        }    };}

在发送方

ArrayList<PuntoNota> myList = new ArrayList<>();// Fill data to myList here...Intent intent = new Intent();intent.putParcelableArrayListExtra("miLista", myList);

在接收方

ArrayList<? extends PuntoNota> listaFinal = getIntent().getParcelableArrayListExtra("miLista");


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/570563.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号