简体   繁体   中英

Unchecked cast java.io.Serializable to 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:

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?

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.

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

At sender side

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

At receiver side

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

You can set a warning Suppression @SuppressWarnings annotation.

Example:

@SuppressWarnings("unchecked")
listaFinal = (ArrayList<PuntoNota>) getIntent().getSerializableExtra("miLista");

It is an annotation to suppress compile warnings about unchecked generic operations (not exceptions), such as casts. It essentially implies that the programmer did not wish to be notified about these which he is already aware of when compiling a particular bit of code.

You can read more on this specific annotation here:

SuppressWarnings

Additionally, Oracle provides some tutorial documentation on the usage of annotations here:

Annotations

As they put it,

"The 'unchecked' warning can occur when interfacing with legacy code written before the advent of generics (discussed in the lesson titled Generics)."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM