我对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);
}
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");
}
无法运作
/** 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);
}
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
}
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);
}
}
最佳答案
当您打开包裹的包装以创建新对象时,就会出现问题。具体来说,您对readStringList()
的呼叫。此方法旨在用包裹中的数据填充现有对象,而不创建新对象。
意识到当打开包裹时,按照Parcel
的定义,将调用以Parcelable.CREATOR
作为参数的构造函数,而不是其他参数化的构造函数。因此,shortVakatName
和vakatActive
都没有初始化为任何东西(它们是空指针)。
您可以通过执行以下两项操作之一来解决此问题,或者让包裹在填充数据时为您创建List
:
private Custom(Parcel in){
alarmTitle = in.readString();
shortVakatName = in.createStringArrayList();
vakatActive = in.createStringArrayList();
alarmType = in.readString();
alarmTime = in.readString();
alarmDate = in.readString();
}
或者,在告诉
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();
}
还要注意,在两个示例中,我都固定了读取顺序以匹配您的
writeToParcel()
方法(您丢失了alarmType
参数,当您通过Intent
传递数据时,这会导致奇怪的结果。高温超导