昨天开发引入的两个错误--Parcelable
以下的代码有两处明显的错误,一不小心就会写出如下错误的代码,其中第2个问题调试起来很麻烦:
1 public class AddedBannerInfo implements Parcelable { 2 3 private List<BannerInfo> mBannerInfoList; 4 5 @Override 6 public int describeContents() { 7 return 0; 8 } 9 10 @Override 11 public void writeToParcel(Parcel dest, int flags) { 12 dest.writeList(mBannerInfoList); 13 } 14 15 public static final Parcelable.Creator<AddedBannerInfo> 16 CREATOR = new Parcelable.Creator<AddedBannerInfo>() { 17 18 @Override 19 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 @Override 28 public AddedBannerInfo[] newArray(int size) { 29 return new GameCenterAddedBannerInfo[size]; 30 } 31 }; 32 }
A.第22行,addedInfo.mBannerInfoList为空不能直接将其作为参数,传给source.readList,而应该在之前先构造下:
addedInfo.mBannerInfoList = new ArrayList<BannerInfo>();
B.第24行,返回的竟然一直是null,这个很有可能是stub代码,应修改为:
return addedInfo;