2

I have the following class:

public class IdList extends ArrayList<Long> {

    public IdList() {
        super();
    }

    public IdList(Collection<Long> idCollection) {
        super(idCollection);
    }
}

I pass an instance of this class to an IntentService with the following code:

final Intent intent = new Intent(this, MyService.class);
intent.putExtra(MyService.IDS, ids);
startService(intent);

And i use the following code to fetch it in the Service:

IdList ids = (IdList) intent.getSerializableExtra(IDS);

Which results in the following ClassCastException:

java.lang.ClassCastException: java.util.ArrayList cannot be cast to my.package.IdList

BUT

Sending an object of the same type (IdList) from the Service by using LocalBroadcastManager and fetching it in onReceive() (via the same method, by calling getSerializableExtra() on the Intent and casting to IdList) works flawlessly.

No ClassCastException thrown.

Why is this happening? Is this a bug in Android?

EDIT:

I'm using the following method to instantiate my IdList object:

public IdList getIds() {
    return selectedIds.isEmpty() ? null : new IdList(selectedIds);
}

selectedIds is a HashSet<Long> object.

EDIT2:

I'm not looking for a workaround. I'd like to know why the same code works perfectly fine when the Intent is sent through a broadcast and why does it throw a ClassCastException when it is passed through the startService() call.

justanoob
  • 1,797
  • 11
  • 27
  • How do you instantiate `ids` before the call to `intent.putExtra(MyService.IDS, ids);`? – gus27 Oct 05 '16 at 14:10
  • Please show your code for instantiating the `ids` variable. – gus27 Oct 05 '16 at 15:05
  • There's a nice article regarding your problem: [The mysterious case of the Bundle and the Map](https://medium.com/the-wtf-files/the-mysterious-case-of-the-bundle-and-the-map-7b15279a794e#.l9yencapv) - should work for ArrayLists, too ;-) It explains the problem and suggests a workaround. – gus27 Oct 05 '16 at 15:27
  • @gus42 Have you read my question? I'm perfectly aware there are several workarounds, but i'm not asking for a workaround. I'm asking why a `ClassCastException` is thrown in the first scenario and why is the same code working without any problems in the second one (when sending the `Intent` as a broadcast). – justanoob Oct 05 '16 at 15:37
  • Here is the explanation to why it happens http://stackoverflow.com/a/25626637/2267723 – Maher Abuthraa Oct 06 '16 at 11:14

1 Answers1

0

I would not recommend using serializing .. use Parcelable instead of that

Make your IdList class Parcelable, then you can get put/get your object

intent.putParcelableArrayList(ids);

IdList ids = (IdList) intent.getParcelableArrayListExtra(IDS);

I hope that may help,'.

Maher Abuthraa
  • 17,493
  • 11
  • 81
  • 103
  • Thanks for your answer but i'm aware of the possible workarounds. I'm not looking for a workaround but asking why is the problem described in my question happening. – justanoob Oct 05 '16 at 15:43