The issue is following - I start the activity and when I want to take a image I call the camera (button onclick) with startActivityForResult(intent) and the intent with bitmap returns as normal where I assign the bitmap to a property in my CounterUser object (it's class implements parcelable) and at last show it on a ImageView - this works fine..but to handle the orientation change I added the following code in the activity - this causes and NullPointerException and closes the activity:
NOTE - counterObj is my instance of the CounterUser class which implements the Parcelable interface.
@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);
}
}
The error:
This is my class implementing the Parcelable interface - it has also a Bitmap property... it uses two constructor - In the activity I use the first no argument constructor to assign null values to all instance properties
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();
}
}