Nested object parcel not working

百般思念 提交于 2020-01-07 00:36:34

问题


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...

Parcel and pojo structure :-

Just showing the variables in pojo..

1) RateRule :-

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

2) Rule :-

 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) Room:-

    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 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];
        }

    };

}

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

回答1:


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.

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.



来源:https://stackoverflow.com/questions/22088805/nested-object-parcel-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!