Android 将整数数组读写到地块

Android 将整数数组读写到地块,android,parcelable,Android,Parcelable,我找不到任何解决方案来处理包裹中的整数数组(我想使用这两个函数dest.writeIntArray(storeId);和in.readIntArray(storeId);) 这是我的密码 public class ResponseWholeAppData implements Parcelable { private int storeId[]; public int[] getStoreId() { return storeId; } pub

我找不到任何解决方案来处理包裹中的整数数组(我想使用这两个函数dest.writeIntArray(storeId);in.readIntArray(storeId);

这是我的密码

public class ResponseWholeAppData implements Parcelable {
    private int storeId[];

    public int[] getStoreId() {
        return storeId;
    }

    public void setStoreId(int[] storeId) {
        this.storeId = storeId;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    public ResponseWholeAppData(){
        storeId = new int[2];
        storeId[0] = 5;
        storeId[1] = 10;
    }

    public ResponseWholeAppData(Parcel in) {

        if(in.readByte() == (byte)1) 
             in.readIntArray(storeId);  //how to do this storeId=in.readIntArray();  ?                          
        }

    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        if(storeId!=null&&storeId.length>0)                   
        {
            dest.writeByte((byte)1);
            dest.writeIntArray(storeId);
        }
        else
            dest.writeByte((byte)0);

    }
    public static Parcelable.Creator<ResponseWholeAppData> getCreator() {
        return CREATOR;
    }

    public static void setCreator(Parcelable.Creator<ResponseWholeAppData> creator) {
        CREATOR = creator;
    }

    public static Parcelable.Creator<ResponseWholeAppData> CREATOR = new Parcelable.Creator<ResponseWholeAppData>()
            {
        public ResponseWholeAppData createFromParcel(Parcel in)
        {
            return new ResponseWholeAppData(in);
        }
        public ResponseWholeAppData[] newArray(int size)
        {
            return new ResponseWholeAppData[size];
        }
            };      
}
公共类响应WhoLeappData实现可包裹{
私有int-storeId[];
public int[]getStoreId(){
返回storeId;
}
公共无效设置存储ID(int[]存储ID){
this.storeId=storeId;
}
@凌驾
公共int描述内容(){
返回0;
}
公共响应WHOLEAPPDATA(){
storeId=新的整数[2];
storeId[0]=5;
storeId[1]=10;
}
公共响应WHOLEAPPDATA(包裹中){
if(in.readByte()==(字节)1)
in.readIntArray(storeId);//如何执行此storeId=in.readIntArray()?
}
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
if(storeId!=null&&storeId.length>0)
{
目标写入字节((字节)1);
目的写入阵列(存储ID);
}
其他的
目标写入字节((字节)0);
}
public static Parcelable.Creator getCreator(){
回归创造者;
}
公共静态无效集合创建者(Parcelable.Creator){
创造者=创造者;
}
public static Parcelable.Creator=新建Parcelable.Creator()
{
公共响应WhoLeapData createFromParcel(包裹中)
{
返回新的ResponseWhoLeapData(在中);
}
公共响应WHOLEAPPDATA[]新数组(整数大小)
{
返回新的ResponseWhoLeapData[大小];
}
};      
}
当我在.readIntArray(storeID)中使用“
”时,我得到一个错误:

“原因:java.lang.NullPointerException 位于android.os.Parcel.readIntArray(Parcel.java:672)”

我没有使用“
readIntArray
”,而是使用了以下内容:

storeID = in.createIntArray();

现在没有错误了。

我假设MyObj类实现了Parcelable并实现了所有必需的方法;在这里,我只建议阅读/书写包裹的细节

如果事先知道阵列大小:

public void writeToParcel(Parcel out, int flags) {
    super.writeToParcel(out, flags);
    out.writeIntArray(mMyIntArray);        // In this example array length is 4
}

protected MyObj(Parcel in) {
    super(in);
    mMyIntArray = new int[4];
    in.readIntArray(mMyIntArray);
}
否则:

public void writeToParcel(Parcel out, int flags) {
    super.writeToParcel(out, flags);
    out.writeInt(mMyArray.length);        // First write array length
    out.writeIntArray(mMyIntArray);       // Then array content
}

protected MyObj(Parcel in) {
    super(in);
    mMyIntArray = new int[in.readInt()];
    in.readIntArray(mMyIntArray);
}

谢谢我会试试这个,然后告诉你。谢谢。6年后,readFloatArray(…)和createFloatArray()解决了同样的问题,并且仍然适用。我一辈子都不明白ReadInArray()为什么抛出NullPointerException。使用上述答案会导致三星设备崩溃