java – 使包含字节数组的自定义Parcelable
发布时间:2020-12-15 04:57:58 所属栏目:Java 来源:网络整理
导读:我正在尝试创建一个包含字节数组的Parcelable类. 我一直在尝试各种各样的事情,但如果失败似乎仍然失败 我想把这个parcelable放在我的意图上. public class Car implements Parcelable{private String numberPlate;private String objectId;private byte[] ph
|
我正在尝试创建一个包含字节数组的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,ba.length);
this.photo = ba;
}
如果我不包括字节数组,它工作正常.. 解决方法
为什么你使用createByteArray?我以一种应该工作的方式修改了你的代码……
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);
}
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- java – EHCache如何检查缓存中是否存在某些内容?
- java – 长度和长度有什么区别?
- 在多线程环境中返回c#中的字典
- java – 无效的列类型?将ArrayList发送到pl / sql createdN
- Java List.remove()方法:移出列表中的指定元素
- java – 资源导入顺序在Spring XML中是否重要?
- JSP PageContext.include()方法:在页面中包含文件
- (de)将Java对象序列化为文件的最佳方法是什么
- java – 在linux中运行startup.sh时被拒绝[已关闭]
- java – jtable cellrenderer在运行时更改单元格的backgrou
