简体   繁体   中英

Parcelable - ArrayList<String> NullPointerException

I'm making my object parcelable so I can use it with Intent s. I'm getting a NullPointerException when, once received, I'm reading my object's content. Most exactly the content of my ArrayList<String> . When iterating over my ArrayList<String> it has null value in all positions, therefore my NullPointerException .

Here's my Parcelable class:

public class PhoneBackup implements Parcelable{

public Integer id;
public Long ts;
public String device_name;
public String device_manufacturer;
public String device_model;
public Integer backup_contacts_count;
public String description;
public ArrayList<String> backup_contacts;

public PhoneBackup()
{

}


public PhoneBackup(Parcel pc){
//      backup_contacts = new ArrayList<String>(); // Tried this way, still NPE

    id = pc.readInt();
    ts =  pc.readLong();
    device_name = pc.readString();
    device_manufacturer = pc.readString();
    device_model = pc.readString();
    backup_contacts_count = pc.readInt();
    description = pc.readString();
//      pc.readStringList(backup_contacts); // Tried this way, still NPE
    backup_contacts = pc.createStringArrayList();
}

// (getters and setters)

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(id);
    dest.writeLong(ts);
    dest.writeString(device_name);
    dest.writeString(device_manufacturer);
    dest.writeString(device_model);
    dest.writeInt(backup_contacts_count);
    dest.writeString(description);
    dest.writeList(backup_contacts);

}

/** Static field used to regenerate object, individually or as arrays */
public static final Parcelable.Creator<PhoneBackup> CREATOR = new Parcelable.Creator<PhoneBackup>() {
    public PhoneBackup createFromParcel(Parcel pc) {
        return new PhoneBackup(pc);
    }
    public PhoneBackup[] newArray(int size) {
        return new PhoneBackup[size];
    }
};

The NullPointerException is with my backup_contacts . But backup_contacts has the correct size but the containts are all null and as I need to read the contents of backup_contacts using get(position) I receive a null value and not a String .

Question is, why is the Parcelable passing null values to the ArrayList<String> and not he actual Strings? How can I fix it?

To read a list it should be

backup_contacts = new ArrayList<String>();
in.readList(backup_contacts, null);

Here is the documentation.

The documentation for createStringArrayList says:

Read and return a new ArrayList containing String objects from the parcel that was written with writeStringList(List)

I think the problem might be in the writing to the parcel :

dest.writeList(backup_contacts);

should be

dest.writeStringList(backup_contacts);

public final void writeStringList (List val) Added in API level 1

Flatten a List containing String objects into the parcel, at the current dataPosition() and growing dataCapacity() if needed. They can later be retrieved with createStringArrayList() or readStringList(List).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM