ArrayList cannot be cast to custom class extending ArrayList
Asked Answered
P

5

6

I have class mentioned below:

public class JsonHistoryList extends ArrayList<JsonHistory> implements Serializable{}

I wish to pass it through intent using

timerService.putExtra(TimerService.ACTIVITY_LIST_ARG, activities);

But after I receive it (way below)

JsonHistoryList temp = (JsonHistoryList) intent.getSerializableExtra(TimerService.ACTIVITY_LIST_ARG);

in my service it gives me exception:

Caused by: java.lang.ClassCastException: java.util.ArrayList cannot be cast to com.epstim.captime.jsonmodel.JsonHistoryList

I don't understand why java can't handle that operation.

I've changed my code in service to:

ArrayList<JsonHistory> temp = (ArrayList<JsonHistory>) intent.getSerializableExtra(TimerService.ACTIVITY_LIST_ARG);
activities.addAll(temp);

And it worked.

Principate answered 2/9, 2014 at 12:9 Comment(4)
try cleaning your project and running it again.Lemcke
what is activities? are you sure is JsonHistoryList ?Disparity
Please print temp.getClass().toString() and tell us what it is.Quinonoid
activities is a list of JsonHistoryList ??Kinsley
E
9

In the internals, android puts every extra in a special Map and doesn't record how exactly you want it parcelled.

At some point android will flattern your extras into parcel, and it will do so by checking each object type (since, as I said, it doesn't remember how you want it).

Parcel supports both writeList and writeSerializable and your JsonHistoryList is also both (list of serializables and a serialisable itself)

So android parcelling goes like this:

for (Object object : extras) {
   //... check for other types here
   } else if (object instanceof List) {
     parcel.writeList(object); // This what happens in your case and will be unparcelled into arraylist
   } else if (object instanceof Serializable) {
     parcel.writeSerializable(object); // What you really want, but percelling never will get here
   }
}

If you want to preserve list you need to create a class that will be serializable and won't extend arraylist but will contain it inside.

public class SuperJsonHistory implements Serializable {
  private JsonHistoryList yourList;
  ...
}

So composition over inheritance in case you want to preserve type

Entrenchment answered 2/9, 2014 at 15:6 Comment(1)
Do you know if Parcelable is above List in this type checking list? I would prefer to have my class inherit ArrayList, if I can!Trafalgar
B
1

This happens if intent.getSerializableExtra(TimerService.ACTIVITY_LIST_ARG); returns an object of class ArrayList<JsonHistory> and not an object of type JSONHistoryList.

We cannot forcefully downcast a parent object.

Consider your example,say

public class JsonHistoryList extends ArrayList implements Serializable{ public int someField ;// can by anything }

For simplicity, if your getSerializableExtra(); returns, new ArrayList<JsonHistory>(), when you try to downcast this to JsonHistoryList, it cannot cast so because someField values cannot be determinied

Barayon answered 2/9, 2014 at 12:13 Comment(3)
But why intent.getSerializableExtra(); returns ArrayList not JsonHistoryList?Principate
I think your activities is of type ArrayList<JsonHistory> and thats why it is returning an ArrayListBarayon
in both files: private JsonHistoryList activities; first is Activity and second ServicePrincipate
P
0

we cannot cast a superclass object into a sub-class object.

Cast is only possible if object which is to be casted passes the IS-A relationship.

you can have a look at this link.It will clear all doubts explicit casting from super class to subclass

Pitch answered 2/9, 2014 at 12:16 Comment(0)
S
0

So if JsonHistoryList extends ArrayList, then a JsonHistoryList is an ArrayList but an ArrayList is not necessarily a JsonHistoryList. You expected your own subclass, but that's not what you got back.

Silici answered 2/9, 2014 at 12:16 Comment(0)
S
0

I met the same problem as you, I just add a wrapper class which implements Serializable:

public class JsonHistoryListWrapper implement Serializable {
    JsonHistoryList jsonHistoryList;

    private JsonHistoryListWrapper(JsonHistoryList jsonHistoryList) {
        this.jsonHistoryList = jsonHistoryList;
    }

    public static JsonHistoryListWrapper wrapper(JsonHistoryList jsonHistoryList) {
        return new JsonHistoryListWrapper(jsonHistoryList);
    }
}
Shirelyshirey answered 23/6, 2019 at 14:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.