gpt4 book ai didi

java - Android 可打包 : RuntimeException: Unmarshalling unknown type code

转载 作者:搜寻专家 更新时间:2023-11-01 08:51:31 25 4
gpt4 key购买 nike

我正在尝试将一个对象从一个 Activity 传递到另一个 Activity 。我遵循了本教程 enter link description here但我得到了异常(exception):

引起:java.lang.RuntimeException:包裹 android.os.Parcel@41ed4e70:在偏移量 332 处解码未知类型代码 6357091

我的类(class)是:

public class Venue implements Parcelable{

private int id;
private String name;
@SerializedName("address")
private String location;
@SerializedName("lat")
private double latitude;
@SerializedName("lon")
private double longitude;
@SerializedName("postal_code")
private String postalCode;
private String country;
private String phone;
private String website;
@SerializedName("foursquare_id")
private String foursquareId;
@SerializedName("location_id")
private int locationId;
private String city;
@SerializedName("Occurrence")
private Occurrence occurrence;

public Venue(){}

public Venue(int id, String name, String location){
this.id = id;
this.name = name;
this.location = location;
}

public Venue(Parcel in){
readFromParcel(in);
}

/**
* Clase para recuperar los datos de un parcel, IMPORTANTE leerlos en el mismo orden que se escribieron!
* @param in Parcel con los datos a leer
*/
private void readFromParcel(Parcel in) {
this.id = in.readInt();
this.name = in.readString();
this.location = in.readString();
this.latitude = in.readDouble();
this.longitude = in.readDouble();
this.occurrence = in.readParcelable(Occurrence.class.getClassLoader());
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(this.id);
dest.writeString(this.name);
dest.writeString(this.location);
dest.writeDouble(this.latitude);
dest.writeDouble(this.longitude);
dest.writeParcelable(this.occurrence, flags);
}

public static final Parcelable.Creator<Venue> CREATOR = new Parcelable.Creator<Venue>() {
public Venue createFromParcel(Parcel in) {
return new Venue(in);
}

public Venue[] newArray(int size) {
return new Venue[size];
}
};

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public double getLatitude() {
return latitude;
}

public void setLatitude(double latitude) {
this.latitude = latitude;
}

public double getLongitude() {
return longitude;
}

public void setLongitude(double longitude) {
this.longitude = longitude;
}

public String getPostalCode() {
return postalCode;
}

public void setPostalCode(String postalCode) {
this.postalCode = postalCode;
}

public Occurrence getOccurrence() {
return occurrence;
}

public void setOccurrence(Occurrence occurrence) {
this.occurrence = occurrence;
}

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

public class Occurrence implements Parcelable{
private int id;
@SerializedName("start_date")
private Date startDate;
@SerializedName("end_date")
private Date endDate;
private Event event;

public Occurrence(Parcel in){
readFromParcel(in);
}

/**
* Clase para recuperar los datos de un parcel, IMPORTANTE leerlos en el mismo orden que se escribieron!
* @param in Parcel con los datos a leer
*/
private void readFromParcel(Parcel in) {
id = in.readInt();
startDate = new Date(in.readLong());
endDate = new Date(in.readLong());
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(id);
dest.writeLong(startDate.getTime());
if (endDate != null)
dest.writeLong(endDate.getTime());
}

public static final Parcelable.Creator<Occurrence> CREATOR = new Parcelable.Creator<Occurrence>() {
public Occurrence createFromParcel(Parcel in) {
return new Occurrence(in);
}

public Occurrence[] newArray(int size) {
return new Occurrence[size];
}
};

public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
public Event getEvent() {
return event;
}
public void setEvent(Event event) {
this.event = event;
}
@Override
public int describeContents() {
return 0;
}

以及当我尝试传递数组时的代码:

Intent intent = new Intent(getActivity().getApplicationContext(), MoreDatesAndPlacesActivity.class);
intent.putParcelableArrayListExtra("venues", (ArrayList<? extends Parcelable>) currentEventModel.getVenues());

以及我尝试获取数组时的代码:

Bundle b = getIntent().getExtras();
List<Venue> venues = b.getParcelableArrayList("venues");

我该如何解决这个异常?谢谢

最佳答案

我猜这是在 Occurrence 对象中的 endDate == null 时发生的。在这种情况下,您没有向 Parcel 写入值,但 readFromParcel() 方法仍在尝试读取它。

也许像下面这样的东西会有所帮助:

private void readFromParcel(Parcel in)
{
id = in.readInt();
startDate = new Date(in.readLong());

long date = in.readLong();
if (date != 0L)
{
endDate = new Date(date);
}
}

public void writeToParcel(Parcel dest, int flags)
{
dest.writeInt(id);
dest.writeLong(startDate.getTime());

if (endDate != null)
{
dest.writeLong(endDate.getTime());
}
else
{
dest.writeLong(0L);
}
}

关于java - Android 可打包 : RuntimeException: Unmarshalling unknown type code,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22926054/

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