我有这个包裹类:
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);
但是,当我通过意图获取包含此变量的项目时,出现此错误:
java.lang.RuntimeException: Parcel android.os.Parcel@d7fbbfb: Clearing the unknown type code 7274612 at offset 788
有人知道如何解决?我已经尝试过ArrayList并以不同的方式进行读写,但从未成功
最佳答案
我没有找到任何可打包的解决方案,但是我使用了可序列化的,并且效果很好:
现在,第一个可包裹类是可序列化的类:
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);