Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat is wrong with my Android class implementing Parcelable? NullPointerException in activity?
    primarykey
    data
    text
    <p>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: </p> <p>NOTE - counterObj is my instance of the CounterUser class which implements the Parcelable interface. </p> <pre><code>@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); } } </code></pre> <p>The error:</p> <p><img src="https://i.stack.imgur.com/rowUW.png" alt="enter image description here"></p> <p>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 </p> <pre><code>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&lt;CounterUser&gt; CREATOR = new Parcelable.Creator&lt;CounterUser&gt;() { 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(); } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload