Не удается решить "Parcelable встретил IOException, пишущий сериализуемый объект" - PullRequest
0 голосов
/ 13 октября 2019

java.lang.RuntimeException: Parcelable обнаружил IOException при записи сериализуемого объекта (name = 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 голосов
/ 13 октября 2019

Я изменил 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);
    }
}
...