Reputation: 1909
I've come across a problem with writing a collection of objects to parcel. I have a collection of MyObject (that is any object which implements Parcelable) stored inside MyRegistry that must be Pacelable as well:
public class MyObject implements Parcelable {...}
public class MyRegistry implements Parcelable {
private List<Parcelable> mRegistry = new ArrayList<Parcelable>();
...
public void writeToParcel(Parcel dest, int flags)
{
dest.writeParcelableArray((Parcelable[])mRegistry.toArray(), flags);
}
}
However the ClassCastException is thrown when writeParcelableArray() is called. So what is wrong with writing MyObject(s) that is Parcelable? Haven't met any restrictions about this in the documentation...
Thank you in advance!
Upvotes: 2
Views: 2154
Reputation: 3210
I think it's because List.toArray() returns an Object[] array, which you then try to cast. You should instead use List.toArray(T[]).
Upvotes: 2