SteMMo
SteMMo

Reputation: 418

Error casting ArrayList

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 .

Upvotes: 0

Views: 105

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503529

What does it mean? I don't understand why the cast is not possible .

It means that the object in question is an ArrayList of some kind, not a Campionati.

Your inheritance relationship means that any Campionati can be treated as an ArrayList - but the reverse is not true. You can only cast to Campionati if the object in question is actually an instance of Campionati or a subclass.

Upvotes: 4

Related Questions