Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 692
Hipólito Huaylla Avatar asked Aug 12 '18 04:08

Hipólito Huaylla


2 Answers

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");
like image 79
Son Truong Avatar answered Nov 15 '22 18:11

Son Truong


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)."

like image 29
Anisuzzaman Babla Avatar answered Nov 15 '22 18:11

Anisuzzaman Babla