7

別の Parcelable insde を持つ Parcelable クラスを実装しています。

OuterParcelable クラス:

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

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

その後:

public OuterParcelable(Parcel parcel) {
    super();

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

と:

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

上記のコードを使用してオブジェクトを再作成すると、次のようになります。

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

2 に答える 2

3

非プリミティブ属性をパーセル可能な、場合によっては null の値として格納するクリーンな方法。Parcel.writeValue()readValue( ) を使用します。以下のコードのコメントを参照してください。

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

魅力のように機能します。writeValue() と readValue() は、可能な null の処理と型検出をカプセル化します。javadoc から:

public final void writeValue (Object v)
一般的なオブジェクトを区画にフラット化します。指定された Object 値は現在、次のいずれかのタイプです:
null、String、Integer、...
String[]、boolean[]、...
Parcelable プロトコルを実装する任意のオブジェクト。...

于 2013-01-03T20:20:21.620 に答える
2

値をバンドルに入れているのはなぜですか? クラスにパーセルブルを完全に実装しましたか?

パーセル可能なスケルトン

public MyClass(Parcel in) {
    readFromParcel(in);
}

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

@Override
public void writeToParcel(Parcel dest, int flags) { 
    dest.writeParcelable(aParcelableClass, flags);
}

private void writeObject(Parcel dest, Object obj) {
    if (obj != null) {
        dest.writeInt(1);
        dest.writeValue(obj);
    } else {
        dest.writeInt(0);
    }
}

public void readFromParcel(Parcel in) {
    aParcelableClass = in.readParcelable(ParcelableClass.class.getClassLoader());
}

private Object readObject(Parcel in) {
    Object value = null;

    if (in.readInt() == 1) {
        value = in.readValue(null); // default classloader
    }

    return value;
}

public static final Parcelable.Creator<MyClass> CREATOR = new Parcelable.Creator<MyClass>() {
    @Override
    public MyClass createFromParcel(Parcel source) {
        return new MyClass(source);
    }

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

null 値を扱いやすくするためにいくつか追加しましたが、原則は同じです。@Override アイテム、コンストラクター、およびクリエーターが必要です。

パーセルブルを読み書きする場合、クラスローダーとして null を指定すると問題が発生します。

于 2012-08-02T12:26:32.217 に答える