问题
I have a problem with getParcelableArrayListExtra and Null Pointer Exception.
Working
My Activity:
/** 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");
}
NOT working
/** 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
}
What is the problem with ArrayList?
Anybody help me?
.....................................................................................................
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);
}
}
回答1:
The problem comes when you are unpacking the parcel to create a new object. Specifically, your calls to readStringList()
. This method is designed to fill an existing object with data from the parcel, not to create a new one.
Realize that when the parcel is unpacked, the constructor that takes a Parcel
as a parameter is being called, per the definition of your Parcelable.CREATOR
, and NOT the other parameterized constructor. Therefore, neither shortVakatName
nor vakatActive
were ever initialized to anything (they are null pointers).
You can fix the issue by doing one of two things, either let the Parcel create the List
for you when inflating the data:
private Custom(Parcel in){
alarmTitle = in.readString();
shortVakatName = in.createStringArrayList();
vakatActive = in.createStringArrayList();
alarmType = in.readString();
alarmTime = in.readString();
alarmDate = in.readString();
}
Or, create the objects before telling Parcel
to fill it with data.
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();
}
Also notice, in both examples, I fixed the reading order to match your writeToParcel()
method (you were missing the alarmType
parameter, which would have led to strange results when you passed your data through the Intent
.
HTH
来源:https://stackoverflow.com/questions/8902380/getparcelablearraylistextra-return-nullpointerexception