无法解决“Parcelable 遇到写入可序列化对象的 IOException”
Posted
技术标签:
【中文标题】无法解决“Parcelable 遇到写入可序列化对象的 IOException”【英文标题】:Can't solve "Parcelable encountered IOException writing serializable object" 【发布时间】:2020-02-10 07:03:52 【问题描述】:java.lang.RuntimeException: Parcelable遇到IOException写 可序列化对象(名称 = com.luzian.recipeapp.Recipe)
我已经搜索过这个问题的答案,但似乎都可以通过让两个类从 Serializable 实现(我正在这样做)来解决,但没有成功。
我的两班菜谱和配料:
public class Recipe implements Serializable
private String name;
private String time;
private String instructions;
private ArrayList<Ingredient> ingredients;
public Recipe(String name, String time, String instructions, ArrayList<Ingredient> ingredients)
this.name = name;
this.time = time;
this.instructions = instructions;
this.ingredients = ingredients;
public class Ingredient implements Serializable
private String value;
private String unit;
private String name;
public Ingredient(String value, String unit, String name)
this.value = value;
this.unit = unit;
this.name = name;
开始新的活动:
Intent intent = new Intent(context, RecipeDisplayActivity.class);
intent.putExtra("recipe", recipes.get(position)); // recipes.get(position) returns a Recipe object
context.startActivity(intent);
【问题讨论】:
你试过实现 Parcelable 吗? @Swayangjit 谢谢!通过实现 Parcelable 而不是 Serializable 我让它工作了。 【参考方案1】:我将 Serializable 更改为 Parcelable 并覆盖了所需的方法 - 现在它可以工作了!
感谢@Swayangjit
public class Recipe implements Parcelable
private String name;
private String time;
private String instructions;
private ArrayList<Ingredient> ingredients;
public Recipe(String name, String time, String instructions, ArrayList<Ingredient> ingredients)
this.name = name;
this.time = time;
this.instructions = instructions;
this.ingredients = ingredients;
protected Recipe(Parcel in)
name = in.readString();
time = in.readString();
instructions = in.readString();
ingredients = in.readArrayList(Ingredient.class.getClassLoader());
public static final Creator<Recipe> CREATOR = new Creator<Recipe>()
@Override
public Recipe createFromParcel(Parcel in)
return new Recipe(in);
@Override
public Recipe[] newArray(int size)
return new Recipe[size];
;
@Override
public int describeContents()
return 0;
@Override
public void writeToParcel(Parcel dest, int flags)
dest.writeString(name);
dest.writeString(time);
dest.writeString(instructions);
dest.writeList(ingredients);
public class Ingredient implements Parcelable
private String value;
private String unit;
private String name;
public Ingredient(String value, String unit, String name)
this.value = value;
this.unit = unit;
this.name = name;
protected Ingredient(Parcel in)
value = in.readString();
unit = in.readString();
name = in.readString();
public static final Creator<Ingredient> CREATOR = new Creator<Ingredient>()
@Override
public Ingredient createFromParcel(Parcel in)
return new Ingredient(in);
@Override
public Ingredient[] newArray(int size)
return new Ingredient[size];
;
@Override
public int describeContents()
return 0;
@Override
public void writeToParcel(Parcel dest, int flags)
dest.writeString(value);
dest.writeString(unit);
dest.writeString(name);
【讨论】:
以上是关于无法解决“Parcelable 遇到写入可序列化对象的 IOException”的主要内容,如果未能解决你的问题,请参考以下文章