嵌套对象包裹不起作用 [英] Nested object parcel not working

查看:57
本文介绍了嵌套对象包裹不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我正在尝试打包嵌套对象,但它给出了 RuntimeException:Parcel:无法编组值....

Hi Guys i am trying to parcel nested object but it gives RuntimeException: Parcel: unable to marshal value....

请帮我解决这个问题...

please help me to solve this problem...

包裹和 pojo 结构:-

Parcel and pojo structure :-

只显示 pojo 中的变量..

Just showing the variables in pojo..

1) 速率规则 :-

public class RateRule{
    private List<Rule> rules = new ArrayList<Rule>();
 }

2) 规则:-

 public class Rule {
    private String ruleID;
    private String splOfferId;
    private String ruleName;
    private String ruleDescription; 
    private String ruleType;
    private List<Room> rooms = new ArrayList<Room>();
 }

3) 房间:-

    public class Room {
        private int id; 
        private String name; 
        private String propertyName;
        private String roomThumbnailUrl; 
        private String hotelInfo;
        private float price; 
        private float discountPrice;
        private String roomTypeId;
        private String maxOccupancy; 
        private List<String> amenities = new ArrayList<String>();   
        private List<String> facilities = new ArrayList<String>(); 
        private List<Gallery> gallery = new ArrayList<Gallery>(); 
        private List<String> tvChannels = new ArrayList<String>();
        private List<String> attractions = new ArrayList<String>();
   }

----------- Parcelable 类来读取或写入 :-

----------- Parcelable class to read or write :-

public class RateRuleParcel  implements Parcelable {
    private RateRule rateRule;

    public RateRule getRateRule() {
        return rateRule;
    }

    public RateRuleParcel(RateRule rateRule) {
        super();
        this.rateRule = rateRule;
    }
    public RateRuleParcel(Parcel in) {
        rateRule=new RateRule();
        ArrayList<Rule> readArrayList = (ArrayList<Rule>)in.readArrayList(Object.class.getClassLoader());
        rateRule.setRules(readArrayList);
    }
    @Override
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        // TODO Auto-generated method stub
         dest.writeList((ArrayList<Rule>)rateRule.getRules());

    }
    public static final Parcelable.Creator<RateRuleParcel> CREATOR = new Parcelable.Creator<RateRuleParcel>() {

        @Override
        public RateRuleParcel createFromParcel(Parcel in) {
            return new RateRuleParcel(in);
        }

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

    };

}

<小时>

以及我试图在包裹中设置 vales 的课程:--


And the class where i am trying to set vales in parcel:--

List<Room> roomItem;    List<Rule> ruleItem;    List<Rule> rateRuleItem; RateRule rateRule = new RateRule();

            Rule rule= new Rule();
            Room room= new Room();
            ruleItem=new ArrayList<Rule>();
            roomItem=new ArrayList<Room>();
            rateRuleItem=new ArrayList<Rule>();
            rule.setRuleID(rateRuleIdArray.get(groupPosition));
            rule.setSplOfferId(splOfferId);
            rule.setRuleName("TESTING");
            rule.setRuleDescription("TESTING");
            rule.setRuleType(rateRuleTypeArray.get(groupPosition));

            room.setId(Integer.parseInt(setId.get(childPosition)));
            room.setName(setName.get(childPosition).toString());
            room.setPropertyName(propertyName);
            room.setRoomThumbnailUrl("TESTING");
            room.setHotelInfo(setHotelInfo.get(childPosition).toString());
            room.setPrice(Float.parseFloat(setPrice.get(childPosition).toString()));
            room.setDiscountPrice(Float.parseFloat(setDiscountPrice.get(childPosition).toString()));
        //  ruleItem.add(rule);
            roomItem.add(room);
            rule.setRooms(roomItem); 
            rateRuleItem.add(rule);
            rateRule.setRules(rateRuleItem);

推荐答案

将打包视为一个递归过程.给定一个实现 Parcelable 接口的任意对象,parcel 进程调用 object.writeToParcel().再次在此方法中,您需要打包对象的所有字段,它们要么是本地可打包类型(int、bool、String、List 等),要么是可打包的.如果它们是parcelables,则对它们调用writeToParcel() 方法.这样做直到在每个递归分支中都达到本机可分块类型.

Think of parceling as a recursive process. Given an arbitrary object that implements the Parcelable interface, the parcel process calls object.writeToParcel(). In this method again, you need to parcel all fields of object, where they are either natively parcelable types (int, bool, String, List, ...) or again parcelables. If they are parcelables, the writeToParcel() method is called on them. You do this until you reach natively parcelable types in every recursion branch.

看看你的代码,用 dest.writeList((ArrayList)rateRule.getRules()); 你依赖的事实是有一个方法似乎写了一个列出一个包裹.但是有一个先决条件是列表元素的类型又是可分块的,这里不是这种情况.您需要使 Rule 可分割,因此您需要使 Room 可分割,因此您需要使 Galery 可分割,...

Having a look at your code, with dest.writeList((ArrayList<Rule>)rateRule.getRules()); you rely on the fact that there is a method that seems to wrote a list to a parcel. But there is the precondition that the type of the list elements is again parcelable, which is not the case here. You need to make Rule parcelable, thus you need to make Room parcelable, thus you need to make Galery parcelable, ...

一般来说,先看看你想被打包的类,然后自上而下检查每个字段的类是否已经可以打包了.

In general, have a look at the class you want to be parceled and then check top-down for each field's class whether it is already parcelable.

这篇关于嵌套对象包裹不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆