问题如下 - 我开始活动,当我想拍摄图像时,我用 startActivityForResult(intent) 调用相机(按钮 onclick),位图的意图正常返回,我将位图分配给 CounterUser 对象中的属性(它的类实现了 parcelable)并最后在 ImageView 上显示它 - 这工作正常..但是为了处理方向更改,我在活动中添加了以下代码 - 这会导致 NullPointerException 并关闭活动:
注意 - counterObj 是我实现 Parcelable 接口的 CounterUser 类的实例。
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putParcelable("counterObj", counterObj);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
if(savedInstanceState != null){
counterObj = savedInstanceState.getParcelable("counterObj");
counterImgView.setImageBitmap(counterObj.counterImg);
}
}
错误:
这是我实现 Parcelable 接口的类 - 它还有一个 Bitmap 属性......它使用两个构造函数 - 在活动中,我使用第一个无参数构造函数将空值分配给所有实例属性
public class CounterUser implements Parcelable {
String fname;
String lname;
String adresse;
Integer counterID;
Integer counterValue;
Bitmap counterImg;
Boolean damageExists;
String damageDescript;
//----
Double longitude;
Double latitude;
Integer workerID;
String updateDate;
public CounterUser(){
this(null,null,null,null,null,null,null,null,null,null,null,null);
}
public CounterUser(String fname, String lname, String adresse, Integer counterID, Integer counterValue, Bitmap counterImg, Boolean damageExists,
String damageDescript, Double longitude, Double latitude, Integer workerID, String updateDate){
this.fname = fname;
this.lname = lname;
this.adresse = adresse;
this.counterID = counterID;
this.counterValue = counterValue;
this.counterImg = counterImg;
this.damageExists = damageExists;
this.damageDescript = damageDescript;
this.longitude = longitude;
this.latitude = latitude;
this.workerID = workerID;
this.updateDate = updateDate;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.fname);
dest.writeString(this.lname);
dest.writeString(this.adresse);
dest.writeInt(this.counterID);
dest.writeInt(this.counterValue);
//Bitmap je parcelable
this.counterImg.writeToParcel(dest, flags);
dest.writeByte((byte) (this.damageExists ? 1 : 0));
dest.writeString(this.damageDescript);
dest.writeDouble(this.longitude);
dest.writeDouble(this.latitude);
dest.writeInt(this.workerID);
dest.writeString(this.updateDate);
}
public static final Parcelable.Creator<CounterUser> CREATOR = new Parcelable.Creator<CounterUser>() {
public CounterUser createFromParcel(Parcel in) {
return new CounterUser(in);
}
public CounterUser[] newArray(int size) {
return new CounterUser[size];
}
};
private CounterUser(Parcel in) {
this.fname = in.readString();
this.lname = in.readString();
this.adresse = in.readString();
this.counterID = in.readInt();
this.counterValue = in.readInt();
//Bitmap je parcelable
this.counterImg = Bitmap.CREATOR.createFromParcel(in);
this.damageExists = in.readByte() == 1;
this.damageDescript = in.readString();
this.longitude = in.readDouble();
this.latitude = in.readDouble();
this.workerID = in.readInt();
this.updateDate = in.readString();
}
}