【问题标题】:Implementing Parcelable for a custom 2D array为自定义二维数组实现 Parcelable
【发布时间】:2014-11-24 19:23:22
【问题描述】:

为了在新活动中传递我的二维数组,我想出了包装它并实现 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;

更新我不再收到空指针异常,但我的数据没有按应有的方式正确写入。 (parceableMazeMap[0][0] != originalMazeMap[0][0])

【问题讨论】:

    标签: android android-intent nullpointerexception parcelable


    【解决方案1】:

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


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

    private MazeMapParcelable(Parcel in){
        ...
    }
    

    但此时您从未真正初始化过您的数组。所以它仍然会处于这种状态(为空):

    private Cell[][] mazeMap;
    

    由于这个状态的迷宫地图没有被初始化,所以它没有长度。所以这段代码不会给你你期望的值:

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

    尝试将宽度和高度值写入 parcelable 中的第一项,然后在重新创建 parcelable 时读取它们。

    【讨论】:

    • 我在更新中发布了我的代码,参考了您的建议(如果我理解正确的话)。我一直在思考 nullPointerException
    • 我按照您的说明进行操作,但仍然遇到同样的问题(我已根据您的说明更新了有问题的代码)。我错过了什么吗?
    • 嗯,好吧...从您的堆栈跟踪中,哪个对象为空?如果是mazeMapParcelable,那么可能在意图中根本不存在额外的内容。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-02
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多