0

I'm trying to read a List of Strings and I keep having the following error:

img

with the following Parcelable class:

import java.util.List;

import android.os.Parcel;
import android.os.Parcelable;

public class ActivePolicies implements Parcelable {

    private List<String> activePolicies;

    public ActivePolicies(List<String> activePolicies) {
        this.activePolicies = activePolicies;
    }

    public List<String> getActivePolicies(){
        return activePolicies;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeStringList(activePolicies);
    }

    public static final Parcelable.Creator<ActivePolicies> CREATOR = new Parcelable.Creator<ActivePolicies>() {
        @Override
        public ActivePolicies createFromParcel(Parcel in) {

            try {
                return new ActivePolicies(in.createStringArrayList());
            } catch (Exception e) {
                e.printStackTrace();
                return null;
            }
        }
        @Override
        public ActivePolicies[] newArray(int size) {
            return new ActivePolicies[size];
        }
    };

}

It keeps giving me the exception on createFromParcel(), but I can't figure out why.
It seems a simple parcelable, but when I'm debugging at readStringList() from the Parcel class, the list is null.
But when I evaluate the value in createStringArrayList() before it's returning, the list is there with the expected values.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

you need to create constructor that accept Parcel

public ActivePolicies(Parcel in) {
    in.readStringList(activePolicies);
}
hakim
  • 3,819
  • 3
  • 21
  • 25
0

in.createStringArrayList()? Why are you creating a new String list when you want to construct your object?

Use this method.

Parcel.readStringList(List<String> list)

Read into the given List items String objects that were written with writeStringList(List) at the current dataPosition().

Recommended: implement a constructor that accepts a Parcel, then, optionally a readFromParcel method.

public class ActivePolicies implements Parcelable {

    private List<String> activePolicies;

    public ActivePolicies() {
        activePolicies = new ArrayList<String>();
    }

    public ActivePolicies(Parcel in) {
        this();
        readFromParcel(in);
    }

   private void readFromParcel(Parcel in) {
      in.readStringList(activePolicies);
   }

In the creator.

return new ActivePolicies(in);
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245