以下的程式碼有兩處明顯的錯誤,一不小心就會寫出如下錯誤的程式碼,其中第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;