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

标签 java android parcelable

我正在尝试将一个对象从一个 Activity 传递到另一个 Activity 。我按照这个教程enter link description here但我遇到了异常(exception):

由以下原因引起:java.lang.RuntimeException:Parcel 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/

相关文章:

java - 应用程序在 Intent Activity 之前意外转移到主要 Activity

Android <= 11 启动画面背景颜色和品牌

android - 如何在 Android Lollipop 版本的 Parcelable 类中添加/保留 Class<?> 对象

android - Activity.isFinishing() 的 fragment 等价物是什么?

kotlin - 在 Kotlin 中,属性不会被序列化为 Parcel

Android:解包导致内存不足异常。如何检查对象?

java - react native : error: cannot find symbol after upgrade

java - 如何检查是否支持 Quartz Extreme?

java - 获取错误 : Unknown service requested [org. hibernate.boot.registry.classloading.spi.ClassLoaderService]

java - 使用 Scala swing 的经验,它如何添加到 Java swing 中已经可用的内容?有什么挑战吗?