嵌套对象包不起作用

时间:2014-02-28 07:22:57

标签: android parcelable parcel

嗨,大家好我试图包裹嵌套对象,但它给出了RuntimeException:Parcel:无法编组值....

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

包裹和pojo结构: -

只是在pojo中显示变量..

1)RateRule: -

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类: -

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的类: -

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 个答案:

答案 0 :(得分:2)

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

看看你的代码,用dest.writeList((ArrayList<Rule>)rateRule.getRules());你依赖的事实是有一个方法似乎把一个列表写入一个包裹。但是前提条件是列表元素的类型再次可以被分配,这不是这里的情况。 您需要将Rule设为parcelable,因此您需要设置Room parcelable,因此您需要将Galery设为parcelable,...

一般来说,看一下你想要被分区的班级,然后自上而下检查每个领域的班级,看它是否已经是可以分割的。