4

I'm trying to make a Parcelable class that contains byte array. I have been trying all sorts of things, but it still seems to fail if I want to put the parcelable to my intent.

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;

}

it works fine if i don't include the byte array..

dumazy
  • 13,857
  • 12
  • 66
  • 113
  • read this http://prasanta-paul.blogspot.in/2010/06/android-parcelable-example.html#comment-form – Akram Jun 05 '12 at 13:24

1 Answers1

12

Why you use createByteArray ?? I revised your code in a way it should work...

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);
}
Rafael T
  • 15,401
  • 15
  • 83
  • 144
  • http://stackoverflow.com/questions/11708945/tranfering-bytearray-through-parcel-returns-nullpointerexception – Akshat Aug 28 '14 at 00:31