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

标签 java android exception runtimeexception serializable

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/

相关文章:

java - 自定义ListView不显示

java - 我可以在 ant 目标中声明属性值并将该值用于另一个目标吗?

java - Android 上的 HTTP Post Multipart 不发布图像。

android - 如何使用 Cmake 在 Android 中使用 GLM

android - SearchView imeOptions 和 onQueryTextSubmit 支持

c# - 调试时,我可以在 LINQ 中查看导致异常的对象吗?

c++ - map/unordered_map 插入期间内存分配失败

java - NetBeans 模块中的操作 - 上下文菜单、主菜单

java - 无法使用 Volley 库中的 Intent 从 fragment 继续下一个 Activity

java - 异常循环