Parcelable Array List不起作用

时间:2015-09-28 18:27:09

标签: android parcelable

我有一个自定义对象和一个数组列表,当我尝试检索一个可分割的数组列表时,我得到一个空异常。自定义对象的代码:

iron-selector

第一个活动的代码部分:

public class Patient implements Parcelable {

private String fullName;
private String id; // this is the Key of the Table Patient
private String address;
private int age;
private String telephone;
private String Doctor;
private String  lastVisit;
private String healthStatus;

//constructor
public Patient(String fullName, String id, String address, int age, String telephone, String doctor, String lastVisit, String healthStatus) {
    this.fullName = fullName;
    this.id = id;
    this.address = address;
    this.age = age;
    this.telephone = telephone;
    Doctor = doctor;
    this.lastVisit = lastVisit;
    this.healthStatus = healthStatus;
}

//Key
public String getId() {
    return id;
}

public String getAddress() {
    return address;
}

public int getAge() {
    return age;
}

public String getTelephone() {
    return telephone;
}

public String getDoctor() {
    return Doctor;
}

public String getFullName() {

    return fullName;
}

public String getLastVisit() {
    return lastVisit;
}

public String getHealthStatus() {
    return healthStatus;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeInt(age);
    dest.writeString(fullName);
    dest.writeString(address);
    dest.writeString(id);
    dest.writeString(telephone);
    dest.writeString(Doctor);
    dest.writeString(String.valueOf(lastVisit));
    dest.writeString(healthStatus);
}

public static final Parcelable.Creator<Patient> CREATOR = new Parcelable.Creator<Patient>() {
    public Patient createFromParcel(Parcel in) {
        return new Patient(in);
    }

    public Patient[] newArray(int size) {
        return new Patient  [size];
    }

};

private Patient(Parcel in) {
    age = in.readInt();
    fullName = in.readString();
    address = in.readString();
    id = in.readString();
    telephone = in.readString();
    Doctor = in.readString();
    fullName = in.readString();
    healthStatus = in.readString();
    lastVisit =  in.readString();
}

}

以及我尝试检索列表的下一个Activity的代码部分:

 if (user != null) {
       Intent intent = new Intent(logIn.this, MainActivity.class);
       if(mPatientList != null){
            for(int i = 0 ; i < mPatientList.size(); i ++){
               if(stUserName.equals(mPatientList.get(i).getDoctor())){
                    UserList.add(mPatientList.get(i));
               }
            }
       Bundle bundle = new Bundle();
       bundle.putParcelableArrayList("patientList", UserList);
       intent.putExtras(bundle);
       startActivity(intent);
       finish();
 }

由于某种原因,Next Activity中的List为null,并且在尝试使用它时会出现异常。

我遵循文档,我无法弄清楚错误的部分在哪里。此外,第一个活动中的列表中包含数据。

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

您将列表放在intent中,但是您尝试在第二个活动中从savedInstanceState检索它。您需要从意图中检索列表。

patientList = getIntent().getParcelableArrayList("patientList"); // May require a cast if you partient list is a specific type