gpt4 book ai didi

java - 读取 Parcelable 类的 ArrayList 或 List 时出现问题

转载 作者:行者123 更新时间:2023-12-01 16:21:27 24 4
gpt4 key购买 nike

我有这个可分割的类:

public class EventInviteWorkingHours implements Parcelable {

int dayOfWeek;
String begin;
String finish;

public EventInviteWorkingHours() {

}

protected EventInviteWorkingHours(Parcel in) {
dayOfWeek = in.readInt();
begin = in.readString();
finish = in.readString();
}

public static final Creator<EventInviteWorkingHours> CREATOR = new Creator<EventInviteWorkingHours>() {
@Override
public EventInviteWorkingHours createFromParcel(Parcel in) {
return new EventInviteWorkingHours(in);
}

@Override
public EventInviteWorkingHours[] newArray(int size) {
return new EventInviteWorkingHours[size];
}
};

public int getDayOfWeek() {
return dayOfWeek;
}

public void setDayOfWeek(int dayOfWeek) {
this.dayOfWeek = dayOfWeek;
}

public String getBegin() {
return begin;
}

public void setBegin(String begin) {
this.begin = begin;
}

public String getFinish() {
return finish;
}

public void setFinish(String finish) {
this.finish = finish;
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(dayOfWeek);
dest.writeString(begin);
dest.writeString(finish);
}
}

在另一个实现 Parcelable 并具有此变量的类中: List<EventInviteWorkingHours> workingHours;

我像这样写入和读取这个变量:

dest.writeTypedList(workingHours);

in.readTypedList(workingHours, EventInviteWorkingHours.CREATOR);

但是,当我按 Intent 获取包含此变量的项目时,会出现此错误:

java.lang.RuntimeException: Parcel android.os.Parcel@d7fbbfb: Clearing the unknown type code 7274612 at offset 788

有人知道怎么解决吗?我已经尝试过使用 ArrayList 并以不同的方式进行写入和读取,但从未起作用

最佳答案

我还没有找到任何关于 Parcelable 的解决方案,但我使用了 Serialized 并且它工作得很好:

第一个可打包类现在是可序列化类:

public class EventInviteWorkingHours implements Serializable {

int dayOfWeek;
String begin;
String finish;

public EventInviteWorkingHours() {

}

public int getDayOfWeek() {
return dayOfWeek;
}

public void setDayOfWeek(int dayOfWeek) {
this.dayOfWeek = dayOfWeek;
}

public String getBegin() {
return begin;
}

public void setBegin(String begin) {
this.begin = begin;
}

public String getFinish() {
return finish;
}

public void setFinish(String finish) {
this.finish = finish;
}
}

在另一个实现 Parcelable 并具有此变量的类中: List<EventInviteWorkingHours> workingHours;

将像这样读取和写入该变量:

workingHours = (List<EventInviteWorkingHours>) in.readSerializable();


dest.writeSerializable((Serializable) workingHours);

关于java - 读取 Parcelable 类的 ArrayList 或 List 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62263289/

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