I'm trying to pass an ArrayList of Person Objects from the MainActivity to SecondActivity to print the details of the person in a custom listView adapter.
The application runs but it crashes when it reachesstartActivity()
to the SecondActivity after passing the ArrayList of Persons.
Person implements Parcelable
public Person(String id, String name) {
this.id = id;
this.name = name;
}
public Person(Parcel in) {
id = in.readString();
name = in.readString();
}
public void writeToParcel(Parcel out, int flags) {
out.writeString(id);
out.writeString(name);
}
public static final Parcelable.Creator<Person> CREATOR = new Parcelable.Creator<Person>()
{
@Override
public Person createFromParcel(Parcel in) {
return new Person(in);
}
@Override
public Person[] newArray(int size) {
return new Person[size];
}};
MainActivity
List<Person> list = new ArrayList<Person>();
public void onButtonClick() {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("personObject", list);
startActivity(intent);
}
SecondClass Activity
protected void onCreate() {
Intent i = getIntent();
ArrayList<Person> person = (ArrayList<Person>) i.getParcelableExtra("personObject");
//Get the Arraylist of Persons from MainActivity
listView.setAdapter(new personAdapter(this, R.layout.person_list_layout, person));
EDIT: stack error trace
E/AndroidRuntime(16698): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testApp/com.example.testApp.SecondActivity}: java.lang.NullPointerException
E/AndroidRuntime(16698): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
E/AndroidRuntime(16698): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
E/AndroidRuntime(16698): at android.app.ActivityThread.access$800(ActivityThread.java:144)
E/AndroidRuntime(16698): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
E/AndroidRuntime(16698): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(16698): at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(16698): at android.app.ActivityThread.main(ActivityThread.java:5146)
E/AndroidRuntime(16698): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(16698): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(16698): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
E/AndroidRuntime(16698): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
E/AndroidRuntime(16698): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(16698): Caused by: java.lang.NullPointerException
E/AndroidRuntime(16698): at com.example.testApp.SecondActivity.onCreate(SecondActivity.java:35)
E/AndroidRuntime(16698): at android.app.Activity.performCreate(Activity.java:5231)
E/AndroidRuntime(16698): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
E/AndroidRuntime(16698): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
EDIT2: Line 35 on error log
listView.setAdapter(new personAdapter(this, R.layout.person_list_layout, person));