2016-06-16 8 views
0

나는 Parcelable로 활동을 통해 데이터를 전달하려고합니다.Android - 소포 : 값을 마샬링 할 수 없습니다.

java.lang.RuntimeException: Parcel: unable to marshal value [email protected] 

당신이 문제가 될 수 있습니다 어떤 생각을 가지고 있습니까 :

public class Player implements Parcelable { 

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

    public Player[] newArray(int size) { 
     return new Player[size]; 
    } 
}; 
private String name; 
private List<Card> listCards; 

public Player(String namePlayer) { 
    name = namePlayer; 
    listCards = new ArrayList<>(); 
} 

private Player(Parcel in) { 
    // This order must match the order in writeToParcel() 
    name = in.readString(); 
    in.readList(listCards, null); 
    // Continue doing this for the rest of your member data 
} 

@Override 
public int describeContents() { 
    return 0; 
} 

@Override 
public void writeToParcel(Parcel dest, int flags) { 
    dest.writeString(name); 
    dest.writeList(listCards); 
} 

이 코드를 실행할 때마다, 나는 오류가 발생합니다 : 이것은 내 코드? 당신이 writeList/readList을 할 수없는 클래스 Parcelable를 구현하지 않습니다 Card 때문에

감사

+0

카드는 별도의 클래스입니까? 그것은'Parcelable'을 구현합니까? – jitinsharma

+0

실제로 카드는 별도의 클래스이지만 Parcelable을 구현하지 않습니다. –

답변

6
@Override 
public void writeToParcel(Parcel dest, int flags) { 
    dest.writeString(name); 
    dest.writeList(listCards); 
} 

. Parcelable 개체 목록에만 적용됩니다.

+0

Parcelable에만 적용 할 수있는 것은 아닙니다. Parcel-ed가 될 수있는 Object의 다른 유형이 아니면 Parcelable으로 다시 변경됩니다. –

+0

@ cricket_007 Serializable은'writeToParcel'에서도 작동합니까? – jitinsharma

+0

'writeList'가 모든 객체를 반복하고'writeValue (Object)'않습니다 - 목록은 https://developer.android.com/reference/android/os/Parcel.html#writeValue(java.lang.Object)에서 찾을 수 있습니다.) –