未选中将java.io.Serializable强制转换为java.util.ArrayList

未选中将java.io.Serializable强制转换为java.util.ArrayList,第1张

概述请帮助,我收到以下消息,在我的以下代码中:listaFinal=(ArrayList<PuntoNota>)getIntent().getSerializableExtra("miLista");AdapterDatosadapter=newAdapterDatos(this,listaFinal);PuntoNota.javapublicclassPuntoNotaimplementsSerializable{privateSt

请帮助,我收到以下消息,在我的以下代码中:

ListaFinal = (ArrayList<PuntoNota>) getIntent().getSerializableExtra("miLista");AdapterDatos adapter = new AdapterDatos(this, ListaFinal);

PuntoNota.java

public class PuntoNota implements Serializable{private String punto;private String nota;public PuntoNota (String punto, String nota){    this.punto = punto;    this.nota = nota;}public String getPunto(){    return punto;}public String getNota(){    return nota;}}

AdapterDatos:

public AdapterDatos(Context context, ArrayList<PuntoNota> puntoNotaList) {    this.context = context;    this.puntoNotaList = puntoNotaList;}

该应用程序运行良好,但我收到以下消息:

Unchecked cast: ‘java.io.Serializable’ to ‘java.util.ArrayList ‘ less … (Ctrl + F1).
about this code: (ArrayList ) getIntent (). getSerializableExtra (“myList”); will it be advisable to delete or hIDe this message?

解决方法:

根本原因:这是来自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");
总结

以上是内存溢出为你收集整理的未选中将java.io.Serializable强制转换为java.util.ArrayList全部内容,希望文章能够帮你解决未选中将java.io.Serializable强制转换为java.util.ArrayList所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1118556.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存