gpt4 book ai didi

java - 无法解决 "Parcelable encountered IOException writing serializable object"

转载 作者:行者123 更新时间:2023-12-01 23:13:45 25 4
gpt4 key购买 nike

java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.luzian.recipeapp.Recipe)

我已经寻找了这个问题的答案,但它们似乎都可以通过让两个类从 Serialized 实现来解决——我正在这样做——但没有成功。

我的两堂课的食谱和成分:

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

开始新 Activity :

Intent intent = new Intent(context, RecipeDisplayActivity.class);
intent.putExtra("recipe", recipes.get(position)); // recipes.get(position) returns a Recipe object
context.startActivity(intent);

最佳答案

我将 Serialized 更改为 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);
}
}

关于java - 无法解决 "Parcelable encountered IOException writing serializable object",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58363495/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com