在实施Parcelable含有其他Parcelable问题 [英] Problem in implementing Parcelable containing other Parcelable

查看:250
本文介绍了在实施Parcelable含有其他Parcelable问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我采取了有另一个Parcelable insde Parcelable类。

在OuterParcelable类:

  @覆盖
公共无效writeToParcel(包裹DEST,INT标志){
    捆绑TMP =新包();

    tmp.putParcelable(innerParcelable,mParcelable);
    dest.writeBundle(TMP);
 

然后:

 公共OuterParcelable(宗地块){
    超();

    叠B = parcel.readBundle();
    mParcelable = b.getParcelable(innerParcelable);
 

 公共OuterParcelable createFromParcel(包裹中){
        返回新OuterParcelable(中);
    }
 

当我使用上述code重建对象,我得到:

  08-18 17:13:08.566:ERROR / AndroidRuntime(15520):android.os.BadParcelableException:产生的原因ClassNotFoundException的解组时:my.package.InnerParcelable
 

解决方案

一个干净的方式来存储非原始的属性parcelable,可能为null,值。使用<一个href="http://developer.android.com/reference/android/os/Parcel.html#writeValue%28java.lang.Object%29"相对=nofollow> Parcel.writeValue()和<一href="http://developer.android.com/reference/android/os/Parcel.html#readValue%28java.lang.ClassLoader%29"相对=nofollow> readValue()。见下文code注释:

 公共类MyParcelableClass实现Parcelable {
    @覆盖
    公共无效writeToParcel(包裹DEST,INT标志){
        dest.writeValue(getIntegerAttribute()); // getIntegerAttribute()返回整型
        dest.writeValue(getDoubleAttribute());
        dest.writeValue(getMyEnumAttribute()); // getMyEnumAttribute()返回一个用户定义枚举
        dest.wrtieValue(getUserClassAttribute()); // UserClass必须以类似的方式实现Parcelable
    }

    私人MyParcelableClass(包裹中){
        setIntegerAttribute((整数)in.readValue(空)); //传递null使用默认的类加载器。确定为整数,字符串,等等。
        setDoubleAttribute((双人间)in.readValue(空)); //强制转换为特定的属性类型
        setEnumAttribute((MyEnum)in.readValue(空));
        setUserClassAttribute((UserClass)in.readValue(UserClass.class.getClassLoader())); //使用特定的类加载器
    }

    @覆盖
    公众诠释describeContents()...

    公共静态最终Parcelable.Creator&LT; ParcelableLocationBean&GT; CREATOR ...
}
 

工程就像一个魅力。 writeValue()和readValue()封装处理可能出现空值和类型检测。从javadoc的:

  

公众最终无效writeValue(宾语)
扁平化的通用对象   在一个包裹。给定对象的价值目前可能是一个   以下几种类型:
空,字符串,整数,...
的String [],   布尔[],...
实现Parcelable协议的对象。 ......

I'm implementing Parcelable class that has another Parcelable insde.

In OuterParcelable class:

@Override
public void writeToParcel(Parcel dest, int flags) {
    Bundle tmp = new Bundle();

    tmp.putParcelable("innerParcelable", mParcelable);
    dest.writeBundle(tmp);

and then:

public OuterParcelable(Parcel parcel) {
    super();

    Bundle b = parcel.readBundle();
    mParcelable = b.getParcelable("innerParcelable");

and:

    public OuterParcelable createFromParcel(Parcel in) {
        return new OuterParcelable(in);
    }

When I recreate object using above code I get:

 08-18 17:13:08.566: ERROR/AndroidRuntime(15520): Caused by: android.os.BadParcelableException: ClassNotFoundException when unmarshalling: my.package.InnerParcelable

解决方案

A clean way to store non-primitive attributes as parcelable, possibly null, values. Use Parcel.writeValue() and readValue(). See comments in code below:

public class MyParcelableClass implements Parcelable {
    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeValue(getIntegerAttribute()); // getIntegerAttribute() returns Integer
        dest.writeValue(getDoubleAttribute());
        dest.writeValue(getMyEnumAttribute()); // getMyEnumAttribute() returns a user defined enum
        dest.wrtieValue(getUserClassAttribute()); //UserClass must implement Parcelable in a similar fashion
    }

    private MyParcelableClass(Parcel in) {
        setIntegerAttribute((Integer)in.readValue(null)); //pass null to use default class loader. Ok for Integer, String, etc.
        setDoubleAttribute((Double)in.readValue(null)); //Cast to your specific attribute type
        setEnumAttribute((MyEnum)in.readValue(null));
        setUserClassAttribute((UserClass)in.readValue(UserClass.class.getClassLoader())); //Use specific class loader
    }

    @Override
    public int describeContents() ...

    public static final Parcelable.Creator<ParcelableLocationBean> CREATOR ...    
}

Works like a charm. writeValue() and readValue() encapsulate the dealing with possible nulls and type detection. From javadoc:

public final void writeValue (Object v)
Flatten a generic object in to a parcel. The given Object value may currently be one of the following types:
null, String, Integer, ...
String[], boolean[], ...
Any object that implements the Parcelable protocol. ...

这篇关于在实施Parcelable含有其他Parcelable问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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