0

I'm trying to pass a custom parceable class which contains an array of another custom parceable class between activities. I tried to follow the advice here, but it didn't work for me.

This is the inner class, it works fine when passed alone:

import android.os.Parcel;
import android.os.Parcelable;

public class EntryImage implements Parcelable {
public int imageId;
public int entryid_id;
public String url;
public int height;


public EntryImage(int imageId, int entryid_id, String url, int height) {
    this.imageId = imageId;
    this.entryid_id = entryid_id;
    this.url = url;
    this.height = height;
}


public EntryImage(Parcel in) {
    readFromParcel(in);
}

@Override
public int describeContents() {
    return 0;
}

@Override
public void writeToParcel(Parcel parcel, int flags) {
    parcel.writeInt(imageId);
    parcel.writeInt(entryid_id);
    parcel.writeString(url);
    parcel.writeInt(height);
}

private void readFromParcel(Parcel in) {
    imageId = in.readInt();
    entryid_id = in.readInt();
    url = in.readString();
    height = in.readInt();
}

public static final Parcelable.Creator<EntryImage> CREATOR = new Parcelable.Creator<EntryImage>() {
    public EntryImage createFromParcel(Parcel in) {
        return new EntryImage(in);
    }
    public EntryImage[] newArray(int size) {
        return new EntryImage[size];
    }
};

}

And this is the outer class. The problem is in the last few lines of readFrom and writeToParcel:

import android.os.Parcel;
import android.os.Parcelable;
import java.util.Arrays;

public class Entry implements Parcelable {
public int entryId;
public String name;
public EntryImage[] images;


public Entry(int entryId, String name, EntryImage[] images) {
    this.entryId = entryId;
    this.name = name;
    this.images = images;

}


public Entry(Parcel in) {
    readFromParcel(in);
}

public EntryImage getEntryImage (int asdf) {
    return images[asdf];
}


@Override
public int describeContents() {
    return 0;
}
public void writeToParcel(Parcel parcel, int flags) {
    parcel.writeInt(entryId);
    parcel.writeString(name);
    parcel.writeParcelableArray(images, flags);
}
private void readFromParcel(Parcel in) {
    entryId = in.readInt();
    name = in.readString();
    //images = in.readTypedArray(EntryImage[].class.getClassLoader());
    Parcelable[] parcelableArray = in.readParcelableArray(EntryImage.class.getClassLoader());
    EntryImage[] images = null;
    if (parcelableArray != null) {
        images = Arrays.copyOf(parcelableArray, parcelableArray.length, EntryImage[].class);
    }
}
public static final Parcelable.Creator<Entry> CREATOR = new Parcelable.Creator<Entry>() {
    public Entry createFromParcel(Parcel in) {
        return new Entry(in);
    }
    public Entry[] newArray(int size) {
        return new Entry[size];
    }
};
}
thekthuser
  • 706
  • 1
  • 12
  • 28

1 Answers1

0

The line in Entry.readFromParcel

    EntryImage[] images = null;

should instead be

    images = null;
thekthuser
  • 706
  • 1
  • 12
  • 28