为自定义2D数组实现Parcelable

时间:2014-11-24 19:23:22

标签: android android-intent nullpointerexception parcelable

为了在新活动中传递我的2D数组,我想出了包装它并实现 Parcelable 接口的方法。不幸的是,当我使用Intent.getParcelableExtra时,我得到java.lang.NullPointerException,这意味着我肯定没有正确实现接口。以下是我的已更新代码

public class MazeMapParcelable implements Parcelable
{
private Cell[][] mazeMap;

public MazeMapParcelable(Cell[][] mazeMap)
{
    this.mazeMap = mazeMap;
}

public Cell[][] getMazeMap()
{
    return this.mazeMap;
}


public static final Parcelable.Creator<MazeMapParcelable> CREATOR
        = new Creator<MazeMapParcelable>() {
    public MazeMapParcelable createFromParcel(Parcel in)
    {
        return new MazeMapParcelable(in);
    }

    public MazeMapParcelable[] newArray(int size)
    {
        return new MazeMapParcelable[size];
    }
};

public void writeToParcel(Parcel dest, int flags)
{
    int width = mazeMap.length;
    dest.writeInt(width);
    int height = mazeMap[1].length;
    dest.writeInt(height);

    for(int i=0;i<width;i++)
    {
        for(int j=0;j<height;j++)
        {
            dest.writeInt(mazeMap[i][j].getCordX());
            dest.writeInt(mazeMap[i][j].getCordY());
            dest.writeByte((byte) (mazeMap[i][j].getNorthWall() ? 1 : 0));
            dest.writeByte((byte) (mazeMap[i][j].getSouthWall() ? 1 : 0));
            dest.writeByte((byte) (mazeMap[i][j].getEastWall() ? 1 : 0));
            dest.writeByte((byte) (mazeMap[i][j].getWestWall() ? 1 : 0));
        }
    }
}

public int describeContents()
{
    return 0;
}

public MazeMapParcelable[] newArray(int size)
{
    return new MazeMapParcelable[size];
}

private MazeMapParcelable(Parcel in)
{
    int width = in.readInt();
    int height = in.readInt();
    Cell[][] recreatedMazeMap = new Cell[width][height];

    for(int i=0;i<width;i++)
    {
        for(int j=0;j<height;j++)
        {
            int cordX = in.readInt();
            int cordY = in.readInt();
            boolean northWall = in.readByte() != 0;
            boolean southWall = in.readByte() != 0;
            boolean westWall = in.readByte() != 0;
            boolean eastWall = in.readByte() != 0;
            Cell currCell = new Cell(cordX,cordY,northWall,southWall,westWall,eastWall);
            recreatedMazeMap[i][j] = currCell;
        }
    }
    mazeMap = recreatedMazeMap;
}

请注意,我的Cell类成员是:

protected int x;
protected int y;

private boolean northWall;
private boolean southWall;
private boolean westWall;
private boolean eastWall;
private boolean bomb;
private boolean deadEnd;

更新我不再获得Null指针异常,但我的数据写得不正确。 (parceableMazeMap[0][0] != originalMazeMap[0][0]

1 个答案:

答案 0 :(得分:1)

也许你想研究一下writeTypedArray()。如果在Cell对象中实现Parcelable,则可以使用writeTypedArray()将它们保存到Parcel中。


当parcelable尝试重新创建您的实例时,它会调用此构造函数:

private MazeMapParcelable(Parcel in){
    ...
}

但是你现在还没有真正初始化你的数组。所以它仍然处于这种状态(为空):

private Cell[][] mazeMap;

由于此状态的mazeMap未初始化,因此没有长度。因此,此代码不会为您提供您期望的值:

int width = this.mazeMap.length;
int height = this.mazeMap[1].length;

尝试写宽度&amp;高度值作为parcelable中的第一项,然后在重新创建parcelable时读取它们。