Java Android Parcelable:RuntimeException:解组未知类型代码

Java Android Parcelable:RuntimeException:解组未知类型代码,java,android,parcelable,Java,Android,Parcelable,我试图将一个对象从一个活动传递到另一个活动。我遵循了本教程,但有一个例外: 原因:java.lang.RuntimeException:Parcel android.os。Parcel@41ed4e70: 在偏移量332处解组未知类型代码6357091 我的课程是: public class Venue implements Parcelable{ private int id; private String name; @SerializedName("address") private

我试图将一个对象从一个活动传递到另一个活动。我遵循了本教程,但有一个例外:

原因: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;
}
公共类场馆实行包裹式{
私有int-id;
私有字符串名称;
@序列化名称(“地址”)
私有字符串位置;
@序列化名称(“lat”)
私人双纬度;
@序列化名称(“lon”)
私人双经度;
@序列化名称(“邮政编码”)
专用字符串后代码;
私人国家;
私人电话;
私人字符串网站;
@序列化名称(“foursquare_id”)
私有字符串foursquared;
@SerializedName(“位置\标识”)
私有地址ID;
私人城市;
@序列化名称(“发生”)
私事;
公共场所(){}
公共场所(整数id、字符串名称、字符串位置){
this.id=id;
this.name=名称;
这个位置=位置;
}
公共场所(包裹内){
从包裹中读取;
}
/**
*这是一个很重要的地方,它是一个很重要的地方!
*@param在包裹中,在一个水平面上
*/
private void readFromParcel(地块位于中){
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());
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
dest.writeInt(此.id);
目的地记录(此名称);
目的地writeString(此位置);
目标写双倍(此纬度);
目的地可写双倍(此经度);
dest.writeparceable(this.occurrence,flags);
}
public static final Parcelable.Creator=新建Parcelable.Creator(){
公共场所createFromParcel(包裹内){
返回新场地(在);
}
公共场所[]新阵列(整数大小){
返回新场地[尺寸];
}
};
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共字符串getName(){
返回名称;
}
公共void集合名(字符串名){
this.name=名称;
}
公共字符串getLocation(){
返回位置;
}
公共void集合位置(字符串位置){
这个位置=位置;
}
公共双纬度(){
返回纬度;
}
公共纬度(双纬度){
这个。纬度=纬度;
}
公共双getLongitude(){
返回经度;
}
公共经度(双经度){
这个经度=经度;
}
公共字符串getPostalCode(){
返回后代码;
}
公共无效setPostalCode(字符串postalCode){
this.postalCode=postalCode;
}
公共事件{
返回事件;
}
公共无效集合发生(发生){
这个事件=事件;
}
@凌驾
公共int描述内容(){
返回0;
}
}

公共类引用实现可包裹{
私有int-id;
@SerializedName(“开始日期”)
私人日期开始日期;
@序列化名称(“结束日期”)
私人日期结束日期;
私人活动;
公共事件(包裹中){
从包裹中读取;
}
/**
*这是一个很重要的地方,它是一个很重要的地方!
*@param在包裹中,在一个水平面上
*/
private void readFromParcel(地块位于中){
id=in.readInt();
startDate=新日期(in.readLong());
endDate=新日期(in.readLong());
}
@凌驾
公共无效写入包裹(包裹目的地,内部标志){
目的地写入(id);
dest.writeLong(startDate.getTime());
如果(endDate!=null)
dest.writeLong(endDate.getTime());
}
public static final Parcelable.Creator=新建Parcelable.Creator(){
公共事件createFromParcel(地块中){
返回新事件(在中);
}
公共事件[]新数组(整数大小){
返回新的事件[大小];
}
};
公共int getId(){
返回id;
}
公共无效集合id(内部id){
this.id=id;
}
公共日期getStartDate(){
返回起始日期;
}
公共作废设置开始日期(日期开始日期){
this.startDate=startDate;
}
公共日期getEndDate(){
返回结束日期;
}
公共无效设置结束日期(日期结束日期){
this.endDate=endDate;
}
公共事件getEvent(){
返回事件;
}
公共无效设置事件(事件){
this.event=事件;
}
@凌驾
公共int描述内容(){
返回0;
}
}

当我试图通过数组时,代码是:

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

intent.putParcelableArrayListExtra(“场所”,ArrayList我猜这是在
事件
对象中的
endDate==null
时发生的。在这种情况下,您没有向包写入值,但
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);
    }
}

您可以使用AndroidStudio插件使类成为可包裹的

谢谢,迈克,这对我来说很有效。但这是一种奇怪的行为,因为当我将事件作为参数传递给某个活动时,它就起作用了。嗯……你确定你传递的
事件
对象没有设置
结束日期吗?因为如果设置了,那么就不会发生异常。我90%确定,这就是我进行验证的原因“if endDate!=null”,因为在此之前我收到了一个异常。啊,这是有道理的。我想知道你为什么只在
endDate
上进行检查。好吧,我认为上面的代码应该可以防止这种情况。问题在于对象的写入和读取顺序……顺序应该是相同的,就像读取文件一样。
Bundle b = getIntent().getExtras();
    List<Venue> venues = b.getParcelableArrayList("venues");
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);
    }
}