未经检查的将 java.io.Serializable 强制转换为 java.util.ArrayList [英] Unchecked cast java.io.Serializable to java.util.ArrayList

查看:50
本文介绍了未经检查的将 java.io.Serializable 强制转换为 java.util.ArrayList的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Help please, I get the following message, in the following code that I have:

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;
}

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

The application is working well, but I get the following message:

未经检查的强制转换:'java.io.Serializable' 到 'java.util.ArrayList'less ... (Ctrl + F1).
关于这段代码:(ArrayList) getIntent().getSerializableExtra("myList");是否建议删除或隐藏此消息?

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.如果程序无法将其转换为您期望的类型,它可能会在运行时抛出 ClassCastException.

Root cause: This is a warning from IDE, getSerializableExtra return a Serializable, and you are trying to convert to ArrayList<PuntoNota>. It might throw ClassCastException at runtime if the programe cannot cast it to your expected type.

解决方案:在android中传递用户定义的对象,你的类应该实现Parcelable而不是Serializable接口.

Solution: In android to pass a user-defined object around, your class should implements Parcelable instead of Serializable interface.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆