getParcelableArrayListExtra返回nullpointerexception

Kol*_*sar 1 android nullpointerexception parcelable

我有getParcelableArrayListExtra和Null Pointer Exception的问题.

工作

我的活动:

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    fetch = new ArrayList<Custom>();
    generateEntries();

    Log.i("fetch", fetch.toString());

    Intent myIntent = new Intent(this, CustomObject.class);
    //myIntent.putParcelableArrayListExtra("my", fetch);
    myIntent.putExtra("my", "name");
    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(myIntent);
}
Run Code Online (Sandbox Code Playgroud)

CustomObject:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.customobject);
    lv = (ListView) findViewById(R.id.listview);

    recievedList = new ArrayList<Custom>();

    in = getIntent();

    String s = bu.getString("my");

    Log.i("s", "s");
}
Run Code Online (Sandbox Code Playgroud)

不工作

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    fetch = new ArrayList<Custom>();
    generateEntries();

    Log.i("fetch", fetch.toString());

    Intent myIntent = new Intent(this, CustomObject.class);
    myIntent.putParcelableArrayListExtra("my", fetch);
    //myIntent.putExtra("my", "name");
    myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(myIntent);
}
Run Code Online (Sandbox Code Playgroud)

CustomObject:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.customobject);
    lv = (ListView) findViewById(R.id.listview);

    recievedList = new ArrayList<Custom>();

    in = getIntent();

    recievedList = in.getParcelableArrayListExtra("my"); // NULL POINTER EXCEPTION
}
Run Code Online (Sandbox Code Playgroud)

ArrayList有什么问题?

有人帮帮我吗?

.................................................. .................................................. .

public class Custom implements Parcelable {

    private String alarmTitle;
    private String alarmType;
    private String alarmTime;
    private String alarmDate;
    private List<String> shortVakatName;
    private List<String> vakatActive;

    public Custom(String entry1, List<String> list1, List<String> list2, String entry3, String entry4, String entry5){

        this.shortVakatName = new ArrayList<String>();
        this.vakatActive = new ArrayList<String>();

        this.alarmTitle = entry1;
        this.shortVakatName = list1;
        this.vakatActive = list2;
        this.alarmType = entry3;
        this.alarmTime = entry4;
        this.alarmDate = entry5;
    }

    private Custom(Parcel in){
        alarmTitle = in.readString();
        in.readStringList(shortVakatName);
        in.readStringList(vakatActive);
        alarmTime = in.readString();
        alarmDate = in.readString();
    }

    public static final Parcelable.Creator<Custom> CREATOR =
            new Parcelable.Creator<Custom>() {

        public Custom createFromParcel(Parcel source) {
            return new Custom(source);
        }

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

    };

    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(alarmTitle);
        dest.writeStringList(shortVakatName);
        dest.writeStringList(vakatActive);
        dest.writeString(alarmType);
        dest.writeString(alarmTime);
        dest.writeString(alarmDate);
    }
}
Run Code Online (Sandbox Code Playgroud)

Dev*_*red 16

当您打开包裹以创建新对象时,问题就出现了.具体来说,你的来电readStringList().此方法旨在使用包中的数据填充现有对象,而不是创建新对象.

实现当解包parcel时Parcel,根据你的定义调用作为Parcelable.CREATOR参数的构造函数,而不是其他参数化构造函数.因此,既没有shortVakatName也没有vakatActive被初始化为任何东西(它们是空指针).

您可以通过执行以下两项操作之一来解决问题,或者让Parcel List在膨胀数据时为您创建:

private Custom(Parcel in){
    alarmTitle = in.readString();
    shortVakatName = in.createStringArrayList();
    vakatActive = in.createStringArrayList();
    alarmType = in.readString();
    alarmTime = in.readString();
    alarmDate = in.readString();
}
Run Code Online (Sandbox Code Playgroud)

或者,在告知Parcel用数据填充之前创建对象.

private Custom(Parcel in){
    shortVakatName = new ArrayList<String>();
    vakatActive = new ArrayList<String>();

    alarmTitle = in.readString();
    in.readStringList(shortVakatName);
    in.readStringList(vakatActive);
    alarmType = in.readString();
    alarmTime = in.readString();
    alarmDate = in.readString();
}
Run Code Online (Sandbox Code Playgroud)

另请注意,在这两个示例中,我修复了阅读顺序以匹配您的writeToParcel()方法(您错过了alarmType参数,当您通过数据传递数据时,这会导致奇怪的结果Intent.

HTH