我上了3节课。系列,季节和剧集。 Series类有一个Season对象列表,Season类有一个Episode对象列表。
我想将Series对象发送到这样的其他活动。
Intent intent = new Intent(context, OtherActivity.class);
intent.putExtra("series", (Parcelable)seriesObject);
但是我得到一个NullPointerException。
以下是实现Parcelable的类。
系列课程:
public class Series implements Parcelable {
private int seasonCount;
public ArrayList<Season>seasons;
private String seriesName;
private int lastSeenSeason = 0;
private int lastSeenEpisode = 0;
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(seasonCount);
dest.writeTypedList(seasons);
dest.writeString(seriesName);
dest.writeInt(lastSeenSeason);
dest.writeInt(lastSeenEpisode);
}
private Series(Parcel in){
seasonCount = in.readInt();
in.readTypedList(seasons, Season.CREATOR);
seriesName = in.readString();
lastSeenSeason = in.readInt();
lastSeenEpisode = in.readInt();
}
public static final Parcelable.Creator<Series> CREATOR = new Parcelable.Creator<Series>() {
public Series createFromParcel(Parcel in) {
return new Series(in);
}
public Series[] newArray(int size) {
return new Series[size];
}
};
季节课:
public class Season implements Parcelable{
private int seasonNo;
private int episodeCount;
public ArrayList<Episode> episodes;
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(seasonNo);
dest.writeInt(episodeCount);
dest.writeTypedList(episodes);
}
private Season(Parcel in){
this.seasonNo = in.readInt();
episodeCount = in.readInt();
in.readTypedList(episodes, Episode.CREATOR);
}
public static final Parcelable.Creator<Season> CREATOR = new Parcelable.Creator<Season>() {
public Season createFromParcel(Parcel in) {
return new Season(in);
}
public Season[] newArray(int size) {
return new Season[size];
}
};
和Episode类:
public class Episode implements Parcelable{
private int episodeNo;
private boolean episodeSeen;
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(episodeNo);
dest.writeByte((byte) (episodeSeen ? 1 : 0));
}
private Episode(Parcel in){
episodeNo = in.readInt();
episodeSeen = in.readByte() != 0;
}
public static final Parcelable.Creator<Episode> CREATOR = new Parcelable.Creator<Episode>() {
public Episode createFromParcel(Parcel in) {
return new Episode(in);
}
public Episode[] newArray(int size) {
return new Episode[size];
}
};
答案 0 :(得分:0)
您应该初始化数组:
public ArrayList<Episode> episodes = new ArrayList<Episode>();
public ArrayList<Season> seasons = new ArrayList<Season>();
否则readTypedList
将失败。