I need to pass to a secondary activity an object defined as:
public class Campionati extends ArrayList<Campionato> implements Serializable {
I’m able to create an extra containing the object.
In the onCreate() function of the secondary activity I go to retrieve the data by the code:
Bundle b = getIntent().getExtras();
if (b!=null) {
Serializable s = b.getSerializable("datiSport");
Campionati m_campionati = (Campionati)s;
Both b and s objects are good and s is typed as ArrayList.
The last command generates an exception:
Java.lang.ClassCastException: Java.util.ArrayList
What does it mean?
I don’t understand why the cast is not possible .
It means that the object in question is an
ArrayListof some kind, not aCampionati.Your inheritance relationship means that any
Campionatican be treated as anArrayList– but the reverse is not true. You can only cast toCampionatiif the object in question is actually an instance ofCampionatior a subclass.