猿问

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

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


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;

}


}

适配器数据:


public AdapterDatos(Context context, ArrayList<PuntoNota> puntoNotaList) {

    this.context = context;

    this.puntoNotaList = puntoNotaList;

}

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


未经检查的强制转换:'java.io.Serializable' 到 'java.util.ArrayList' 少......(Ctrl + F1)。

关于这段代码:(ArrayList) getIntent()。getSerializableExtra("myList"); 是否建议删除或隐藏此消息?


阿波罗的战车
浏览 285回答 2
2回答

四季花海

根本原因:这是来自 IDE 的警告,getSerializableExtra返回 a Serializable,而您正尝试转换为ArrayList<PuntoNota>. 如果程序无法将其转换为您期望的类型,它可能会在运行时抛出ClassCastException。解决方案:在android中传递用户定义的对象,你的类应该实现Parcelable而不是Serializable接口。class PuntoNota implements Parcelable {&nbsp; &nbsp; private String punto;&nbsp; &nbsp; private String nota;&nbsp; &nbsp; public PuntoNota(String punto, String nota) {&nbsp; &nbsp; &nbsp; &nbsp; this.punto = punto;&nbsp; &nbsp; &nbsp; &nbsp; this.nota = nota;&nbsp; &nbsp; }&nbsp; &nbsp; protected PuntoNota(Parcel in) {&nbsp; &nbsp; &nbsp; &nbsp; punto = in.readString();&nbsp; &nbsp; &nbsp; &nbsp; nota = in.readString();&nbsp; &nbsp; }&nbsp; &nbsp; public String getPunto() {&nbsp; &nbsp; &nbsp; &nbsp; return punto;&nbsp; &nbsp; }&nbsp; &nbsp; public String getNota() {&nbsp; &nbsp; &nbsp; &nbsp; return nota;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int describeContents() {&nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void writeToParcel(Parcel dest, int flags) {&nbsp; &nbsp; &nbsp; &nbsp; dest.writeString(punto);&nbsp; &nbsp; &nbsp; &nbsp; dest.writeString(nota);&nbsp; &nbsp; }&nbsp; &nbsp; public static final Creator<PuntoNota> CREATOR = new Creator<PuntoNota>() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public PuntoNota createFromParcel(Parcel in) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new PuntoNota(in);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public PuntoNota[] newArray(int size) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new PuntoNota[size];&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; };}在发送方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");

慕娘9325324

您可以设置警告抑制@SuppressWarnings注释。例子:@SuppressWarnings("unchecked") listaFinal&nbsp;=&nbsp;(ArrayList<PuntoNota>)&nbsp;getIntent().getSerializableExtra("miLista");这是一个注释,用于抑制有关未经检查的通用操作(不是异常)的编译警告,例如强制转换。它本质上意味着程序员不希望收到有关他在编译特定代码位时已经知道的这些信息的通知。您可以在此处阅读有关此特定注释的更多信息:禁止警告此外,Oracle 在此处提供了一些有关注释使用的教程文档:注释正如他们所说,“在与泛型出现之前编写的遗留代码交互时,可能会出现‘未经检查’警告(在题为泛型的课程中讨论)。”
随时随地看视频慕课网APP

相关分类

Java
我要回答