thoughtbot / expandable-recycler-view

Custom Android RecyclerViewAdapters that collapse and expand
https://robots.thoughtbot.com/introducing-expandablerecyclerview
MIT License
2.12k stars 403 forks source link

ExpandableGroup writeToParcel() fatal exception #104

Closed skullnis closed 1 month ago

skullnis commented 7 years ago

I'm getting this exception when trying to parcel an empty group:

Fatal Exception: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at java.util.ArrayList.get(ArrayList.java:308) at com.thoughtbot.expandablerecyclerview.models.ExpandableGroup.writeToParcel(ExpandableGroup.java:67) at pl.itaka.itaka.models.Destination.writeToParcel(Destination.java:88)

The problem is that you are trying to get the first element of an empty list at line 67:

https://github.com/thoughtbot/expandable-recycler-view/blob/master/expandablerecyclerview/src/main/java/com/thoughtbot/expandablerecyclerview/models/ExpandableGroup.java

antonyhaman commented 5 years ago

To solve this you have to extend the ExpandableGroup with youwr own custom class and override the writeToParcel() method with the folowing one:

@Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(title);
        if (items == null) {
            dest.writeByte((byte) (0x00));
            dest.writeInt(0);
        } else {
            dest.writeByte((byte) (0x01));
            dest.writeInt(items.size());
            if (items.size() > 0) {
                final Class<?> objectsType = items.get(0).getClass();
                dest.writeSerializable(objectsType);
            }
            dest.writeList(items);
        }
    }

I've just added items.size() > 0 check so that items.get(0) will not be called on an empty collection.