Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a class with nested objects Parcelable

I'd like to make class A Parcelable.

public class A {     public String str;     public ArrayList<B> list; } 

This is what I've come up with so far. However it crashes with a NullPointerException. The problem are these two statements: dest.writeList(list); & in.readList(list, this.getClass().getClassLoader());. I can't figure out what to do from here :(

Class A

public class A implements Parcelable {     public String str;     public ArrayList<B> list;      @Override     public int describeContents() {         // TODO Auto-generated method stub         return 0;     }      @Override     public void writeToParcel(Parcel dest, int flags) {         dest.writeString(str);         dest.writeList(list);     }      private A(Parcel in) {         str = in.readString();         in.readList(list, this.getClass().getClassLoader());     }      public static final Parcelable.Creator<A> CREATOR             = new Parcelable.Creator<A>() {         public A createFromParcel(Parcel in) {             return new A(in);         }          public A[] newArray(int size) {             return new A[size];         }     }; } 

Class B

public class B implements Parcelable {     public String str;      @Override     public int describeContents() {         // TODO Auto-generated method stub         return 0;     }      @Override     public void writeToParcel(Parcel dest, int flags) {         dest.writeString(str);     }      private B(Parcel in) {         str = in.readString();     }      public static final Parcelable.Creator<B> CREATOR             = new Parcelable.Creator<B>() {         public B createFromParcel(Parcel in) {             return new B(in);         }          public B[] newArray(int size) {             return new B[size];         }     }; } 

Thank you for your time.

like image 703
Snæbjørn Avatar asked Jan 06 '13 02:01

Snæbjørn


People also ask

What is Parcelable class in Java?

Creating a Parcelable class is a vital skill for Android Developers because it allows you to pass an object from one Activity to another. This series will walk you through step by step in the process of implementing a parcelable class and using it in a simple App.

How do I make a data class Parcelable?

There are 3 ways you can make your class Parcelable: Implementing the Parcelable interface and defining the serialization yourself (The traditional way) Using Android Studio plugins, like Android Parcelable code generator. Using annotation-based libraries, like Parceler.

What is a Parcelable object?

A Parcelable is the Android implementation of the Java Serializable. It assumes a certain structure and way of processing it. This way a Parcelable can be processed relatively fast, compared to the standard Java serialization.


2 Answers

I finally figured out what to type into Google :), and found this Android, How to use readTypedList method correctly in a Parcelable class?

The solution was to use read-/writeTypedList instead. I also initialize the arraylist to avoid any further NullPointerException.

Class A

public class A implements Parcelable {     public String str;     public ArrayList<B> list = new ArrayList<B>();      @Override     public int describeContents() {         // TODO Auto-generated method stub         return 0;     }      @Override     public void writeToParcel(Parcel dest, int flags) {         dest.writeString(str);         dest.writeTypedList(list);     }      private A(Parcel in) {         str = in.readString();         in.readTypedList(list, B.CREATOR);     }      public static final Parcelable.Creator<A> CREATOR             = new Parcelable.Creator<A>() {         public A createFromParcel(Parcel in) {             return new A(in);         }          public A[] newArray(int size) {             return new A[size];         }     }; } 

Class B

public class B implements Parcelable {     public String str;      @Override     public int describeContents() {         // TODO Auto-generated method stub         return 0;     }      @Override     public void writeToParcel(Parcel dest, int flags) {         dest.writeString(str);     }      private B(Parcel in) {         str = in.readString();     }      public static final Parcelable.Creator<B> CREATOR             = new Parcelable.Creator<B>() {         public B createFromParcel(Parcel in) {             return new B(in);         }          public B[] newArray(int size) {             return new B[size];         }     }; } 
like image 110
Snæbjørn Avatar answered Oct 09 '22 23:10

Snæbjørn


If you have only one Parcelable object inside your main Parcelable object, not list like the accepted answer case. Then it will be like the following:

Class A

public class A implements Parcelable {     public String str;     public B objectB;      @Override     public int describeContents() {         // TODO Auto-generated method stub         return 0;     }      @Override     public void writeToParcel(Parcel dest, int flags) {         //The parcelable object has to be the first one         dest.writeParcelable(objectB, flags);         dest.writeString(str);     }      private A(Parcel in) {         this.objectB = in.readParcelable(B.class.getClassLoader());         str = in.readString();     }      public static final Parcelable.Creator<A> CREATOR             = new Parcelable.Creator<A>() {         public A createFromParcel(Parcel in) {             return new A(in);         }          public A[] newArray(int size) {             return new A[size];         }     }; } 

Class B

public class B implements Parcelable {     public String str;      @Override     public int describeContents() {         // TODO Auto-generated method stub         return 0;     }      @Override     public void writeToParcel(Parcel dest, int flags) {         dest.writeString(str);     }      private B(Parcel in) {         str = in.readString();     }      public static final Parcelable.Creator<B> CREATOR             = new Parcelable.Creator<B>() {         public B createFromParcel(Parcel in) {             return new B(in);         }          public B[] newArray(int size) {             return new B[size];         }     }; } 

IMPORTANT: Please note that the order that you write and read the Parcelable object matters. Checkout this answer for more details

like image 28
Sami Eltamawy Avatar answered Oct 09 '22 22:10

Sami Eltamawy