【问题标题】:RuntimeException: Parcel: unable to marshal value androidRuntimeException:包裹:无法编组值android
【发布时间】:2015-11-07 05:22:55
【问题描述】:

我想将模型对象发送到另一个活动,其中包含引用另一个模型的列表

public class row_my_bookings implements Parcelable {

String BookingID;
ArrayList<MainService> list_of_mainservice;

 public row_my_bookings(Parcel in) {
     String[] data = new String[3];
    in.readStringArray(data);
       this.list_of_mainservice = in.readArrayList(null);
}

@Override
public void writeToParcel(Parcel dest, int flags) {

 dest.writeList(this.list_of_mainservice);   // Error shows in this line
}

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

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

row_main_services 模型类

public class row_main_services implements Parcelable{

String ServiceId, 
 public MainService(Parcel in) {
    String[] data = new String[3];
    in.readStringArray(data);
    this.id = in.readString();
    this.name=in.readString();
    if (in.readByte() == 0x01) {
        list_child_services = new ArrayList<row_child_services>();
        in.readList(list_child_services, row_child_services.class.getClassLoader());
    } else {
        list_child_services = null;
    }
   }

@Override
public void writeToParcel(Parcel dest, int flags) {

    dest.writeString(this.id);
    dest.writeString(this.name);
    if (list_child_services == null) {
        dest.writeByte((byte) (0x00));
    } else {
        dest.writeByte((byte) (0x01));
        dest.writeList(list_child_services);
    }
}

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

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

第三模块

public class row_child_services implements Parcelable{

String subId,subName;
public row_child_services(Parcel in) {
    String[] data = new String[3];

    in.readStringArray(data);
    this.subId = in.readString();
    this.subName = in.readString();
 }
 @Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(this.subId);
    dest.writeString(this.subName);      
}
 public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
    public row_child_services createFromParcel(Parcel in) {
        return new row_child_services(in);
    }

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

我正在像以下方式发送对象

list_Bookings=new ArrayList<row_my_bookings>(rowItems);
intentBookingWindow.putParcelableArrayListExtra("BookingObject", list_Bookings);

【问题讨论】:

  • 在“row_my_bookings”类中,您有一个名为“MainService”而不是“row_child_services”的类的 ArrayList。 “MainService”类必须实现“Parcelable”
  • 是的,我也是这样,你说的,还是报错
  • 我已经编辑了我的答案。请看一下,并像我发送的其他两个一样更改 row_child_services 类
  • 也送你的第三堂课;)
  • 它现在给了我“错误的数组长度”

标签: android arraylist model reference parcelable


【解决方案1】:

row_my_bookings 类实现了Parcelable,但我猜MainService 类没有实现Parcelable,这就是你的问题。你也必须让这个课程Parcelable

我建议您像这样更改模型: 您已导入“MainService”类,这不是您稍后发送的类。我想您必须将此类更改为 row_main_services ,然后重新考虑您的 Parcelable 实现;)

public class row_my_bookings implements Parcelable {

String BookingID;
ArrayList<row_main_services> list_of_mainservice; // i guess you must change this line 

protected row_my_bookings(Parcel in) {
    BookingID = in.readString();
    if (in.readByte() == 0x01) {
        list_of_mainservice = new ArrayList<MainService>();
        in.readList(list_of_mainservice, MainService.class.getClassLoader());
    } else {
        list_of_mainservice = null;
    }
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(BookingID);
    if (list_of_mainservice == null) {
        dest.writeByte((byte) (0x00));
    } else {
        dest.writeByte((byte) (0x01));
        dest.writeList(list_of_mainservice);
    }
}

@SuppressWarnings("unused")
public static final Parcelable.Creator<row_my_bookings> CREATOR = new Parcelable.Creator<row_my_bookings>() {
    @Override
    public row_my_bookings createFromParcel(Parcel in) {
        return new row_my_bookings(in);
    }

    @Override
    public row_my_bookings[] newArray(int size) {
        return new row_my_bookings[size];
    }
};
}

clss row_main_services:

public class row_main_services implements Parcelable {

String ServiceId, 
ArrayList<row_child_services> list_row_child_services;

protected row_main_services(Parcel in) {
    if (in.readByte() == 0x01) {
        list_row_child_services = new ArrayList<row_child_services>();
        in.readList(list_row_child_services, row_child_services.class.getClassLoader());
    } else {
        list_row_child_services = null;
    }
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    if (list_row_child_services == null) {
        dest.writeByte((byte) (0x00));
    } else {
        dest.writeByte((byte) (0x01));
        dest.writeList(list_row_child_services);
    }
}

@SuppressWarnings("unused")
public static final Parcelable.Creator<row_main_services> CREATOR = new Parcelable.Creator<row_main_services>() {
    @Override
    public row_main_services createFromParcel(Parcel in) {
        return new row_main_services(in);
    }

    @Override
    public row_main_services[] newArray(int size) {
        return new row_main_services[size];
    }
};
}

这是你的第三节课:

public class row_child_services implements Parcelable {

String subId;
String subName;

    protected row_child_services(Parcel in) {
        subId = in.readString();
        subName = in.readString();
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(subId);
        dest.writeString(subName);
    }

    @SuppressWarnings("unused")
    public static final Parcelable.Creator<row_child_services> CREATOR = new Parcelable.Creator<row_child_services>() {
        @Override
        public row_child_services createFromParcel(Parcel in) {
            return new row_child_services(in);
        }

        @Override
        public row_child_services[] newArray(int size) {
            return new row_child_services[size];
        }
    };
}

关于你遇到的最后一个错误,我建议你看看this的答案。

【讨论】:

  • 这个解决方案没用,我一共有3个模型,一个“父模型”,包含arraylist引用“主模型”,其中包含另一个列表引用“子模型”
  • 你也可以发送你的 MainService 类
  • 实现 Parcelable,Parcelable ,编译时出错,“Duplicate Class: 'Android.os.Parcelable'”
  • 只删除其中一个 ;)
猜你喜欢
  • 2016-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-19
  • 2011-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多