使用savedInstanceState.getParcelableArrayList时出现java.lang.NullPointerException

时间:2019-02-09 09:21:32

标签: nullpointerexception onsaveinstancestate

我正在使用item创建自定义数组。然后,我尝试旋转屏幕,应用程序崩溃,显示错误:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'int java.util.ArrayList.size()'。我尝试搜索错误,发现这行代码不起作用:

    arrayListPatientInfo = savedInstanceState.getParcelableArrayList ("key");

我尝试从许多来源寻求帮助,但没有结果。我想知道我错了吗?

代码项:

    public class RecyclerViewItemPatientInfo implements Parcelable {
private String hotenbn;
private String tenbenh;
private String namsinh;
private String khoadt;
private String ngayvaovien;
private String macongdan;

public RecyclerViewItemPatientInfo(String hotenbn, String tenbenh, String namsinh, String khoadt, String ngayvaovien, String macongdan) {
    this.hotenbn = hotenbn;
    this.tenbenh = tenbenh;
    this.namsinh = namsinh;
    this.khoadt = khoadt;
    this.ngayvaovien = ngayvaovien;
    this.macongdan = macongdan;
}

protected RecyclerViewItemPatientInfo(Parcel in) {
    hotenbn = in.readString();
    tenbenh = in.readString();
    namsinh = in.readString();
    khoadt = in.readString();
    ngayvaovien = in.readString();
    macongdan = in.readString();
}

public static final Creator<RecyclerViewItemPatientInfo> CREATOR = new Creator<RecyclerViewItemPatientInfo>() {
    @Override
    public RecyclerViewItemPatientInfo createFromParcel(Parcel in) {
        return new RecyclerViewItemPatientInfo(in);
    }

    @Override
    public RecyclerViewItemPatientInfo[] newArray(int size) {
        return new RecyclerViewItemPatientInfo[size];
    }
};

public String getHotenbn() {
    return hotenbn;
}

public void setHotenbn(String hotenbn) {
    this.hotenbn = hotenbn;
}

public String getTenbenh() {
    return tenbenh;
}

public void setTenbenh(String tenbenh) {
    this.tenbenh = tenbenh;
}

public String getNamsinh() {
    return namsinh;
}

public void setNamsinh(String namsinh) {
    this.namsinh = namsinh;
}

public String getKhoadt() {
    return khoadt;
}

public void setKhoadt(String khoadt) {
    this.khoadt = khoadt;
}

public String getNgayvaovien() {
    return ngayvaovien;
}

public void setNgayvaovien(String ngayvaovien) {
    this.ngayvaovien = ngayvaovien;
}

public String getMacongdan() {
    return macongdan;
}

public void setMacongdan(String macongdan) {
    this.macongdan = macongdan;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(hotenbn);
    dest.writeString(tenbenh);
    dest.writeString(namsinh);
    dest.writeString(khoadt);
    dest.writeString(ngayvaovien);
    dest.writeString(macongdan);
}}

代码活动:

    public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

// recyclerview
private RecyclerView recyclerViewPatientInfo;
private RecyclerViewAdapterPatientInfo adapterPatientInfo;
private ArrayList<RecyclerViewItemPatientInfo> arrayListPatientInfo;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    arrayListPatientInfo = new ArrayList<>();
    if (savedInstanceState == null) {

        // add data to arrayListPatientInfo
        dataAdd();

    } else {
        arrayListPatientInfo = savedInstanceState.getParcelableArrayList("key");
    }

    createRecyclerView();

}

private void createRecyclerView() {
    // Code create recyclerview
}
private void dataAdd() {
    // Code add data to arraylist
}

// Save && restore data on rotate screen
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
    super.onSaveInstanceState(outState, outPersistentState);
    outState.putParcelableArrayList("key", arrayListPatientInfo);
}}

1 个答案:

答案 0 :(得分:0)

好,任务完成

我在活动〜清单中添加了android:configChanges =“ keyboardHidden | orientation | screenSize”,并且目前所有工作正常。