以下的代码有两处明显的错误,一不小心就会写出如下错误的代码,其中第2个问题调试起来很麻烦:
1 public class AddedBannerInfo implements Parcelable { 2 3 private ListmBannerInfoList; 4 5 @Override 6 public int describeContents() { 7 return 0; 8 } 9 10 @Override11 public void writeToParcel(Parcel dest, int flags) {12 dest.writeList(mBannerInfoList);13 }14 15 public static final Parcelable.Creator 16 CREATOR = new Parcelable.Creator () {17 18 @Override19 public AddedBannerInfo createFromParcel(Parcel source) {20 AddedBannerInfo addedInfo = new AddedBannerInfo();21 22 source.readList(addedInfo.mBannerInfoList, 23 BannerInfo.class.getClassLoader());24 return null;25 }26 27 @Override28 public AddedBannerInfo[] newArray(int size) {29 return new GameCenterAddedBannerInfo[size];30 }31 };32 }
A.第22行,addedInfo.mBannerInfoList为空不能直接将其作为参数,传给source.readList,而应该在之前先构造下:
addedInfo.mBannerInfoList = new ArrayList();
return addedInfo;