简体   繁体   English

可拆分-ArrayList <String> 空指针异常

[英]Parcelable - ArrayList<String> NullPointerException

I'm making my object parcelable so I can use it with Intent s. 我正在使对象可拆分,以便可以与Intent一起使用。 I'm getting a NullPointerException when, once received, I'm reading my object's content. 当收到我正在读取对象内容的内容时,我将收到NullPointerException Most exactly the content of my ArrayList<String> . 最精确地是我的ArrayList<String> When iterating over my ArrayList<String> it has null value in all positions, therefore my NullPointerException . 当遍历我的ArrayList<String>它在所有位置都具有null值,因此,我的NullPointerException

Here's my Parcelable class: 这是我的Parcelable课程:

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 . NullPointerException与我的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 . 但是backup_contacts具有正确的大小,但是包含的内容全为null并且当我需要使用get(position)读取backup_contacts的内容时,我收到的是null值而不是String

Question is, why is the Parcelable passing null values to the ArrayList<String> and not he actual Strings? 问题是,为什么Parcelable将空值传递给ArrayList<String>而不是实际的String? 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: createStringArrayList的文档说:

Read and return a new ArrayList containing String objects from the parcel that was written with writeStringList(List) 从用writeStringList(List)编写的包裹中读取并返回一个新的ArrayList,其中包含String对象。

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 公共最终无效writeStringList(List val)在API级别1中添加

Flatten a List containing String objects into the parcel, at the current dataPosition() and growing dataCapacity() if needed. 在当前dataPosition()处将包含String对象的List展平到包裹中,并在需要时扩大dataCapacity()。 They can later be retrieved with createStringArrayList() or readStringList(List). 以后可以使用createStringArrayList()或readStringList(List)检索它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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