gpt4 book ai didi

java - Android 可打包 : How to read/write string array lists and integer arrays

转载 作者:太空宇宙 更新时间:2023-11-03 12:07:28 26 4
gpt4 key购买 nike

我在尝试将字符串数组列表和整数数组写入包裹时遇到问题。

这是我的类字段

String uniqueDate;
ArrayList <String> set1Numbers;
ArrayList <String> set2Numbers;
ArrayList <String> UIDs;
int[] selectedStatus;

这部分是将数据写入parcel

public void writeToParcel(Parcel dest, int flags) 
{
dest.writeString(uniqueDate);
dest.writeStringList(set1Numbers);
dest.writeStringList(set2Numbers);
dest.writeStringList(UIDs);
dest.writeIntArray(selectedStatus);
}

这部分读它。我觉得问题出在这里

PNItems(Parcel in)
{
this.uniqueDate = in.readString();
in.readStringList(set1Numbers);
in.readStringList(set2Numbers);
in.readStringList(UIDs);
in.readIntArray(selectedStatus);
}

谁能告诉我怎么做,我在网上找不到解决我问题的教程。

最佳答案

如果你看the documentation对于 Parcel,readStringList() 显示:

Read into the given List items String objects that were written with writeStringList(List) at the current dataPosition().

Returns A newly created ArrayList containing strings with the same data as those that were previously written.

它要求您传入的 List<> 是非空的(因为它将填充它)。由于这是您的构造函数,我希望您的参数为空,这就是您崩溃的原因。您应该改用 Parcel#createStringArrayList()返回一个新列表:

Read and return a new ArrayList containing String objects from the parcel that was written with writeStringList(List) at the current dataPosition(). Returns null if the previously written list object was null.

Returns A newly created ArrayList containing strings with the same data as those that were previously written.

例如:

set1Numbers = in.createStringArrayList();

关于java - Android 可打包 : How to read/write string array lists and integer arrays,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25455878/

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