【问题标题】:Android type mismatch in readFromParcel methodreadFromParcel 方法中的 Android 类型不匹配
【发布时间】:2012-07-13 11:56:22
【问题描述】:

编辑: 在下面的答案的帮助下,我现在不再收到类型不匹配错误,但现在我收到消息“- 令牌上的语法错误 >”,无效名称 - 语法错误on token ">",在这个 token 之后的表达式"

edit在我清理项目时解决了新错误。

这是我的代码

public class Game implements Parcelable{

private ArrayList<Stone> allStones;

public Game(){
    allStones = new ArrayList<Stone>();
    for(int x=0; x<10; x++) {
        for(int y=0; y<10; y++) {
            if((x+y)%2 == 1 && y<4){
                Stone stone = new Stone(x, y, Stone.WHITE);
                allStones.add(stone);
            } else if((x+y)%2 == 1 && y>5){
                Stone stone = new Stone(x, y, Stone.BLACK);
                allStones.add(stone);
            }
        }
    }
}

public Game(Parcel in) {
    allStones = new ArrayList<Stone>();
    readFromParcel(in);
}

public ArrayList<Stone> getAllStones() {
    return allStones;
}

public void removeFromStones(Stone stone) {
    allStones.remove(stone);
}

public int describeContents() {
    return 0;
}

public void writeToParcel(Parcel dest, int flags) {
    dest.writeTypedList(allStones);
}

private void readFromParcel(Parcel in) {
    in.readTypedList(allStones, Stone.CREATOR); //This line has the error in it
}
}

还有石头课

public class Stone implements Parcelable{
private int x, y, color;
private Boolean king;

public static final int BLACK = 0;
public static final int WHITE = 1;

public Stone(int x, int y, int color) {
    this.x = x;
    this.y = y;
    this.color = color;
    this.king = false;
}

public Stone(Parcel in) {
    readFromParcel(in);
}

public int getX() {
    return x;
}

public int getY() {
    return y;
}

public int getColor() {
    return color;
}

public boolean getKing() {
    return king;
}

public void setKing() {
    king = true;
}

public void setXY(int x, int y) {
    this.x = x;
    this.y = y;
}

public int describeContents() {
    return 0;
}

public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(x);
    dest.writeInt(y);
    dest.writeInt(color);
    dest.writeByte((byte) (king ? 1:0));
}

public void readFromParcel(Parcel in) {
    x = in.readInt();
    y = in.readInt();
    color = in.readInt();
    king = in.readByte() == 1;
}

public final static Creator<Stone> CREATOR = new Parcelable.Creator<Stone>() {

    public Stone createFromParcel(Parcel source) {
        return new Stone(source);
    }

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

【问题讨论】:

    标签: android arraylist parcelable


    【解决方案1】:

    readTypedList() 不返回值。它将对象列表放入您作为第一个参数传递的列表中。您的代码应如下所示:

    private void readFromParcel(Parcel in) {
        in.readTypedList(allStones, Stone.CREATOR); // Should work now
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-26
      • 2021-06-24
      • 1970-01-01
      • 2023-03-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-29
      相关资源
      最近更新 更多