3

I just changed all serializable classes in my project to parcelable classes. Everything is working fine, except for that one class which is made of an ArrayList containing another ArrayList. I already debugged. There is no error when writing this ArrayList. But I get an error while reading it.

This is the class where the error happens:

public class Timetable implements Parcelable
{
private int actLap = 0;
private ArrayList<Lap> timetable;
private ArrayList<Location> loggedLocations;
private Date startTime; ...
...
public void writeToParcel(Parcel out, int flags)
{
    out.writeInt(actLap);
    out.writeTypedList(timetable); //no error here
    out.writeTypedList(loggedLocations); ...
...
    private Timetable(Parcel in)
{
    actLap          = in.readInt();
    in.readTypedList(timetable, Lap.CREATOR); //error after this line
    in.readTypedList(loggedLocations, Location.CREATOR);
    startTime       = (Date)in.readSerializable();
    bestLap         = in.readParcelable(Lap.class.getClassLoader());
    track           = in.readParcelable(Track.class.getClassLoader());
    description     = in.readString();
}

Here are the other classes:

public class Lap implements Parcelable
{   
private ArrayList<Time> sectorTimes = new ArrayList<Time>();
private Time laptime; ...
...
public void writeToParcel(Parcel out, int flags)
{
    out.writeTypedList(sectorTimes);
    out.writeParcelable(laptime,flags);
}

public static final Parcelable.Creator<Lap> CREATOR = new Parcelable.Creator<Lap>()
{
    public Lap createFromParcel(Parcel in)
    {
        return new Lap(in);
    }

    public Lap[] newArray(int size)
    {
        return new Lap[size];
    }
};

private Lap(Parcel in)
{
    in.readTypedList(sectorTimes, Time.CREATOR);
    laptime     = in.readParcelable(Time.class.getClassLoader());
}

And this class:

public class Time implements Parcelable
{
private long timeLong;
private String timeString;...
...
public void writeToParcel(Parcel out, int flags)
{
    out.writeLong(timeLong);
    out.writeString(timeString);
}

public static final Parcelable.Creator<Time> CREATOR = new Parcelable.Creator<Time>()
{
    public Time createFromParcel(Parcel in)
    {
        return new Time(in);
    }

    public Time[] newArray(int size)
    {
        return new Time[size];
    }
};

private Time(Parcel in)
{
    timeLong = in.readLong();
    timeString = in.readString();
}

Like I already said, everything works fine (there are more Parcelable classes which I pass with intents (including a single ArrayList) and which I save in files). So can you guys help me to write and read that double ArrayList? Thanks in advance

0

1 Answer 1

6

If your code is as posted, then you're just missing the initialization of the ArrayLists, i.e.

private Timetable(Parcel in)
{
    // readTypeList() needs an existing List<> to load.
    timetable = new ArrayList<Lap>();
    loggedLocations = new ArrayList<Location>();

    actLap          = in.readInt();
    in.readTypedList(timetable, Lap.CREATOR);
    in.readTypedList(loggedLocations, Location.CREATOR);
    ...
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! That solved my problem. I had in mind that this method would create a new Arraylist.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.