편집 : 나는 sharp
과 rect
혼란 이름을 가지고 있었다, 그래서 나는 그냥 이런 식으로 구현 혼란을
을 피하기 위해 그들을 교환했습니다
public static final Parcelable.Creator<Rect> CREATOR
= new Parcelable.Creator<Rect>() {
public Rect createFromParcel(Parcel in) {
return new Rect(in);
}
public Rect[] newArray(int size) {
return new Rect[size];
}
};
을 당신은 최우선 없습니다 필드는 Sharp
입니다. 정적 필드를 재정의 할 수 없기 때문입니다. 당신은 필드를 숨기고 있습니다. 사람이 쓴다면 그 의미
Parcelable.Creator<Sharp> creator = Sharp.CREATOR;
참고 :
Parcelable.Creator<Rect> creator = Rect.CREATOR;
는 사람이 쓰는 경우가 아닌 다른 객체를 가져 나는 당신의 클래스 이름
Rect
및
Sharp
을 활용의 자유를했다. 당신도해야합니다. 그것은 당신의 인생을 더 쉽게 만들 것이다 당신의 코드가 더 자바 ;-)
편집과 같이 표시됩니다 : OP의 발언 후 일부 추가 설명을 추가
내가 당신의 문제를 볼 생각합니다. Sharp
개체를 전달하는 인터페이스를 정의하려고하지만 실제로 파생 된 Rect
개체를 전달하려는 경우이 개체는 수신 끝의 Sharp
개체로 다시 만들어집니다. AIDL은 이러한 종류의 상속을 지원하지 않는 것 같습니다.
Rect
에서 :
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(getClass().getName());
// Write other instance variables
}
Sharp
에서 그러나, 당신이 할 수있는 것은이 (가난한 사람의 공장) 같은 것입니다
public static final Parcelable.Creator<Sharp> CREATOR
= new Parcelable.Creator<Sharp>() {
public Sharp createFromParcel(Parcel in) {
String derivedClassName = in.readString();
if (derivedClassName.equals(Rect.class.getName()) {
// Create an instance of Rect and return that
return new Rect(in);
} else if (derivedClassName.equals(...) {
// Possible other derived classes
}
}
public Sharp[] newArray(int size) {
return new Sharp[size];
}
};
주 생성자
public Rect(Parcel in)
에게 그
은 inc에서 이미 삭제 된 클래스 이름으로 호출됩니다. 오밍 Parcel
.
Parcelable 대신 Serializable을 사용할 수 있습니까? Serializable은 구현하기가 매우 쉽습니다. – mtmurdock