2012-06-05 2 views
3

바이트 배열이 포함 된 Parcelable 클래스를 만들려고합니다. 나는 모든 종류의 일을 시도해 왔지만, 여전히 실패한 것처럼 보입니다. 필자의 의도대로 필자를 넣고 싶습니다.바이트 배열을 포함하는 사용자 지정 Parcelable 만들기

public class Car implements Parcelable{ 

private String numberPlate; 
private String objectId; 
private byte[] photo; 
private String type; 
private ParseGeoPoint location; 
private ParseUser owner; 
private String brand; 
private double pricePerHour; 
private double pricePerKm; 
public static final String TYPES[] = {"Cabrio", "Van", "SUV", "Station", "Sedan", "City", "Different"}; 


public Car(String numberPlate, byte[] bs, String type, ParseGeoPoint location, String brand) { 

    this.numberPlate = numberPlate; 
    this.photo = bs; 
    this.type = type; 
    this.brand = brand; 
    this.setLocation(location); 
    this.owner = ParseUser.getCurrentUser(); 
} 

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

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

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



public int describeContents() { 
    // TODO Auto-generated method stub 
    return 0; 
} 

public void writeToParcel(Parcel dest, int flags) { 
    dest.writeString(type); 
    dest.writeString(numberPlate); 
    dest.writeString(brand); 
    dest.writeDouble(pricePerHour); 
    dest.writeDouble(pricePerKm); 
    dest.writeString(objectId); 
    dest.writeByteArray(photo); 


} 

public void readFromParcel(Parcel in){ 
    this.type = in.readString(); 
    this.numberPlate = in.readString(); 
    this.brand = in.readString(); 
    this.pricePerHour = in.readDouble(); 
    this.pricePerKm = in.readDouble(); 
    this.objectId = in.readString(); 
    byte[] ba = in.createByteArray(); 
    in.unmarshall(ba, 0, ba.length); 
    this.photo = ba; 

} 

내가 바이트 배열을 포함하지 않는 경우 잘 작동 ..

당신이 createByteArray를 사용하는 이유
+0

읽어 ... 그것은 작동해야하는 방식으로 코드를 수정이 http://prasanta-paul.blogspot.in/2010/06/android-parcelable-example.html#comment-form – Akram

답변

11

? 내가

public void writeToParcel(Parcel dest, int flags) { 
    dest.writeString(type); 
    dest.writeString(numberPlate); 
    dest.writeString(brand); 
    dest.writeDouble(pricePerHour); 
    dest.writeDouble(pricePerKm); 
    dest.writeString(objectId); 
    dest.writeInt(photo.length()); 
    dest.writeByteArray(photo); 
} 



public void readFromParcel(Parcel in){ 
    this.type = in.readString(); 
    this.numberPlate = in.readString(); 
    this.brand = in.readString(); 
    this.pricePerHour = in.readDouble(); 
    this.pricePerKm = in.readDouble(); 
    this.objectId = in.readString(); 
    this.photo = new byte[in.readInt()]; 
    in.readByteArray(this.photo); 
} 
+0

HTTP : //stackoverflow.com/questions/11708945/tranfering-bytearray-through-parcel-returns-nullpointereception – Akshat

관련 문제