无法解决“ Parcelable遇到IOException编写可序列化对象”

时间:2019-10-13 12:19:34

标签: java android exception runtimeexception serializable

  

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

1 个答案:

答案 0 :(得分:0)

我将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);
    }
}