2012-07-13 3 views
3

< Arraylist를 포함하는 개체에서 parcelable을 만들고 싶습니다. 그러나 내 readFromParcel 메서드에서 오류가 발생합니다. 형식이 일치하지 않습니다. void에서 ArrayList로 변환 할 수 없습니다. 내 소포에서 ArrayList를 제대로 읽으려면 어떻게해야합니까?안드로이드 형식이 readFromParcel 메서드에서 일치하지 않습니다.

편집 : 아래의 답변의 도움으로 이제 더 이상 형식 불일치 오류가 없지만, 지금은 메시지 얻을 "- 토큰에 구문 오류"> "잘못된 이름 - 토큰에 구문 오류"> ",이 토큰 다음에 예상되는 표현식"

편집 프로젝트를 정리할 때 새로운 오류가 해결되었습니다.

여기에 내 코드

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

답변

2

readTypedList()입니다. 첫 번째 매개 변수로 전달한 목록에 개체 목록을 넣습니다. 코드는 다음과 같아야합니다.

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