gpt4 book ai didi

java - 对象的 Parcelable Arraylist

转载 作者:行者123 更新时间:2023-11-29 06:32:46 37 4
gpt4 key购买 nike

我正在尝试将 Person 对象的 ArrayList 从 MainActivity 传递到 SecondActivity,以在自定义 listView 适配器中打印此人的详细信息。

应用程序运行,但在传递 ArrayList of Persons 后到达 SecondActivity startActivity() 时崩溃。

人实现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];
}};

主要 Activity

List<Person> list = new ArrayList<Person>();

public void onButtonClick() {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putExtra("personObject", list);
startActivity(intent);
}

二等 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));

编辑:堆栈错误跟踪

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:错误日志中的第 35 行

listView.setAdapter(new personAdapter(this, R.layout.person_list_layout, person));

最佳答案

ArrayList 没有实现Parcelable,但是它实现了Serializable,所以你不能使用getParcelableExtra来接收数据,你必须使用getSerializableExtra 代替。

ArrayList<Person> person = (ArrayList<Person>) i.getSerializableExtra("personObject");

Person类也必须实现Serializable,下面是代码示例:

Person implements Serializable{
private static final long serialVersionUID = 0L;
String id;
String name;
}

更新:

另一个解决方案:使用putParcelableArrayListExtragetParcelableArrayListExtra

第一个 Activity :

ArrayList<Person> list = new ArrayList<Person>();

public void onButtonClick() {
Intent intent = new Intent(getApplicationContext(), SecondActivity.class);
intent.putParcelableArrayListExtra("personObject", list);
startActivity(intent);
}

第二个 Activity :

ArrayList<Person> person = (ArrayList<Person>) i.getParcelableArrayListExtra("personObject");

注意:ParcelableSerializable快,所以如果你想传递大量数据,第二种方案更好。

关于java - 对象的 Parcelable Arraylist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28733439/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com