Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ArrayList cannot be cast to custom class extending ArrayList

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.

like image 967
Averus Avatar asked Sep 02 '14 12:09

Averus


People also ask

Can a class extend ArrayList string >?

As many other have said, yes, you can extend class ArrayList , but it is not something that you should normally do; it is not considered good practice in Java.

What is the error of ClassCastException?

Introduction. ClassCastException is a runtime exception raised in Java when we try to improperly cast a class from one type to another. It's thrown to indicate that the code has attempted to cast an object to a related class, but of which it is not an instance.

Why we get class cast exception?

This exception is rise automatically by JVM whenever we try to improperly typecast a class from one type to another i.e when we're trying to typecast parent object to child type or when we try to typecast an object to a subclass of which it is not an instance.

What is custom ArrayList?

In Java, array and ArrayList are the well-known data structures. An array is a basic functionality provided by Java, whereas ArrayList is a class of Java Collections framework. It belongs to java. util package.


2 Answers

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

like image 148
Alex Orlov Avatar answered Oct 06 '22 07:10

Alex Orlov


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

like image 27
Anoop Avatar answered Oct 06 '22 06:10

Anoop