当我从
Parcel创建一个对象时,当我试图读回一个String []时,我得到一个NullPointerException.这是我的代码:
@Override public void writeToParcel(Parcel out,int flags) { out.writeInt(floors); out.writeStringArray(indoorMaps); } public static final Parcelable.Creator<Building> CREATOR = new Parcelable.Creator<Building>() { public Building createFromParcel(Parcel in) { return new Building(in); } public Building[] newArray(int size) { return new Building[size]; } }; private Building(Parcel in) { floors = in.readInt(); in.readStringArray(indoorMaps); }
所以indoorMaps是我的类的属性,它是一个String [],但我得到NullPointerException.我检查了dev’s documentation,但那里什么都没有.
我跟着this tutorial,他们在那里使用readStringArray.
有什么建议?谢谢
解决方法
调用readStringArray时,您正在为Parcel提供一个空数组.要使其工作,您必须初始化indoorMaps.你可能想要createStringArray.