次のクラスは 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)