Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android type mismatch in readFromParcel method

< want to make a parcelable from an Object that contains an Arraylist, But in my readFromParcel method I get the error Type Mismatch: cannot convert from void to ArrayList. What can I do to read my ArrayList properly from my parcel?

edit: With the help of the answers below I now no longer get a type mismatch error, but now I get the message"- Syntax error on token ">", invalid Name - Syntax error on token ">", Expression expected after this token"

edit New errors were resolved when I cleaned the project.

Here's my code

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
}
}

And the Stone class

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];
    }
};
}
like image 566
M.Smit Avatar asked Dec 06 '25 10:12

M.Smit


1 Answers

readTypedList() does not return a value. It puts the list of objects into the list you pass as the first parameter. You code should look like this:

private void readFromParcel(Parcel in) {
    in.readTypedList(allStones, Stone.CREATOR); // Should work now
}
like image 138
David Wasser Avatar answered Dec 07 '25 23:12

David Wasser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!