以下类实现Parcelable类

package mobile.bh.classes;

import java.util.ArrayList;
import java.util.List;

import mobile.bh.activities.MethodStep;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;

//simple class that just has one member property as an example
public class Recipe implements Parcelable {
    public int id;
    public String name;
    public List<Ingredient> ingredients;
    public List<MethodStep> method;
    public String comment;
    public String image;
    public Bitmap image2;
    Context mContext;



    public Recipe(Context c){
        mContext = c;
    }
    /* everything below here is for implementing Parcelable */
    public int describeContents() {
        return 0;
    }

    // write your object's data to the passed-in Parcel
    public void writeToParcel(Parcel out, int flags) {
        out.writeInt(id);
        out.writeString(name);
        out.writeList(ingredients);
        out.writeList(method);
        out.writeString(comment);
        out.writeString(image);
    }

    // this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
    public static final Parcelable.Creator<Recipe> CREATOR = new Parcelable.Creator<Recipe>() {
        public Recipe createFromParcel(Parcel in) {
            return new Recipe(in);
        }

        public Recipe[] newArray(int size) {
            return new Recipe[size];
        }
    };
    // example constructor that takes a Parcel and gives you an object populated with it's values
    private Recipe(Parcel in) {
        id = in.readInt();
        name =in.readString();
        ingredients = (ArrayList<Ingredient>)in.readSerializable();
        method = (ArrayList<MethodStep>)in.readSerializable();
        comment = in.readString();
        image = in.readString();
    }
}


但是当读取name时出现以下错误:

09-03 16:15:35.364: E/AndroidRuntime(330): Caused by: java.lang.RuntimeException: Parcelable encountered IOException reading a Serializable object (name = )
09-03 16:15:35.364: E/AndroidRuntime(330):  at android.os.Parcel.readSerializable(Parcel.java:2022)
09-03 16:15:35.364: E/AndroidRuntime(330):  at mobile.bh.classes.Recipe.<init>(Recipe.java:61)
09-03 16:15:35.364: E/AndroidRuntime(330):  at mobile.bh.classes.Recipe.<init>(Recipe.java:56)
09-03 16:15:35.364: E/AndroidRuntime(330):  at mobile.bh.classes.Recipe$1.createFromParcel(Recipe.java:48)
09-03 16:15:35.364: E/AndroidRuntime(330):  at mobile.bh.classes.Recipe$1.createFromParcel(Recipe.java:1)
09-03 16:15:35.364: E/AndroidRuntime(330):  at android.os.Parcel.readParcelable(Parcel.java:1981)
09-03 16:15:35.364: E/AndroidRuntime(330):  at android.os.Parcel.readValue(Parcel.java:1846)
09-03 16:15:35.364: E/AndroidRuntime(330):  at android.os.Parcel.readListInternal(Parcel.java:2092)
09-03 16:15:35.364: E/AndroidRuntime(330):  at android.os.Parcel.readArrayList(Parcel.java:1536)
09-03 16:15:35.364: E/AndroidRuntime(330):  at android.os.Parcel.readValue(Parcel.java:1867)
09-03 16:15:35.364: E/AndroidRuntime(330):  at android.os.Parcel.readMapInternal(Parcel.java:2083)
09-03 16:15:35.364: E/AndroidRuntime(330):  at android.os.Bundle.unparcel(Bundle.java:208)
09-03 16:15:35.364: E/AndroidRuntime(330):  at android.os.Bundle.getParcelableArrayList(Bundle.java:1144)
09-03 16:15:35.364: E/AndroidRuntime(330):  at android.content.Intent.getParcelableArrayListExtra(Intent.java:3448)
09-03 16:15:35.364: E/AndroidRuntime(330):  at mobile.bh.activities.RecipeInfoActivity.onCreate(RecipeInfoActivity.java:56)
09-03 16:15:35.364: E/AndroidRuntime(330):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-03 16:15:35.364: E/AndroidRuntime(330):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

最佳答案

使用时

    out.writeList(ingredients);
    out.writeList(method);


要将数据写入Parcel,您应该使用

    ingredients = in.readList();
    method = in.readList();


阅读它。而不是readSerializable()

祝好运

08-16 22:00