Unchecked cast java.io.Serializable to java.util.ArrayList
Clash Royale CLAN TAG#URR8PPP
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?
2 Answers
2
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.
getSerializableExtra
Serializable
ArrayList<PuntoNota>
Solution: In android to pass a user-defined object around, your class should implements Parcelable
instead of Serializable
interface.
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];
;
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.
@SuppressWarnings
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)."
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
It's just warning message.
– Mạnh Quyết Nguyễn
Aug 12 at 4:29