【问题标题】:Android Parcelable: RuntimeException: Unmarshalling unknown type codeAndroid Parcelable: RuntimeException: Unmarshalling unknown type code
【发布时间】:2014-04-08 01:33:48
【问题描述】:

我正在尝试将一个对象从一个活动传递到另一个活动。我按照本教程 enter link description here 进行操作,但遇到了异常:

原因:java.lang.RuntimeException: Parcel android.os.Parcel@41ed4e70: 在偏移量 332 处解组未知类型代码 6357091

我的课程是:

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");

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

【问题讨论】:

    标签: java android parcelable


    【解决方案1】:

    我猜想当endDate == null 在您的Occurrence 对象中时会发生这种情况。在这种情况下,您没有向 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);
        }
    }
    

    【讨论】:

    • 谢谢迈克,这对我有用。但这是一个奇怪的行为,因为当我将一个 Occurrence 作为参数传递给一个活动时,它起作用了。
    • 嗯...您确定您传递的Occurrence 对象没有设置endDate 吗?因为如果确实如此,则不会发生该异常。
    • 我有 90% 的把握,这就是为什么我将验证设置为“如果 endDate != null”,因为在此之前我收到了一个异常。
    • 啊,有道理。我想知道你为什么只在endDate 上进行检查。好吧,我认为上面的代码应该可以防止这种情况发生。
    【解决方案2】:

    您可以使用 android studio 插件将 class 设为 parcelable https://plugins.jetbrains.com/plugin/7332?pr=

    【讨论】:

    • 问题在于对象的写入和读取顺序...顺序应该相同,就像读取文件一样。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-17
    • 1970-01-01
    • 2015-10-19
    • 1970-01-01
    • 2022-08-22
    • 1970-01-01
    • 2022-11-11
    相关资源
    最近更新 更多