I'm trying to fetch data of this json api
and the first main activity fetched well with name of each recipe but when I click on item to start the detail activity the app just crashed and the eror caused by:
java.lang.IllegalStateException: Bad magic number for Bundle: 0x0
main activity
@Override
public void onClick(BakingItem recipe) {
ArrayList<BakingItem> selectedRecipe = new ArrayList<>();
selectedRecipe.add(recipe);
Context context = this;
Class destinationClass = DetailsActivity.class;
Intent intentToStartDetailActivity = new Intent(context, destinationClass);
Bundle selectedRecipeBundle = new Bundle();
selectedRecipeBundle.putParcelableArrayList("recipe", selectedRecipe);
intentToStartDetailActivity.putExtras(selectedRecipeBundle);
startActivity(intentToStartDetailActivity);
}
main fragment bundle:
Bundle recipesBundle = new Bundle();
recipesBundle.putParcelableArrayList(ALL_RECIPES, recipes);
detail activity:
if (savedInstanceState == null) {
Bundle recipeDetails = getIntent().getExtras();
RecipeDetailsFragment recipeDetailsFragment = new RecipeDetailsFragment();
recipeDetailsFragment.setArguments(recipeDetails);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.add(R.id.recipe_details, recipeDetailsFragment)
.commit();
}
}
detail fragment:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_recipe_details, container, false);
final TextView RecipeName = (TextView) view.findViewById(R.id.recipe_name);
// final TextView Ingredients = (TextView) view.findViewById(R.id.ingredients);
String recipeName;
recipe = new ArrayList<>();
if(savedInstanceState != null) {
recipe = savedInstanceState.getParcelableArrayList("recipe");
}
else {
recipe = getArguments().getParcelableArrayList("recipe");
}
recipeName=recipe.get(0).getName();
RecipeName.setText(recipeName);
I pass the name of recipe from main to detail and received it well but when I add other details to parcelable class like steps array the app start crashed and this is the parcelable class:
public class BakingItem implements Parcelable {
private int id;
private String name;
private ArrayList<StepsItem> steps = null;
private int servings;
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeInt(id);
out.writeString(name);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("stepsList", steps);
out.writeBundle(bundle);
out.writeInt(servings);
}
public BakingItem() {
}
@Override
public int describeContents() {
return 0;
}
public static final Parcelable.Creator<BakingItem> CREATOR = new Parcelable.Creator<BakingItem>() {
@Override
public BakingItem createFromParcel(Parcel in) {
BakingItem bakingItem = new BakingItem();
bakingItem.id = in.readInt();
bakingItem.name = in.readString();
Bundle bundle1 = in.readBundle(StepsItem.class.getClassLoader());
bakingItem.steps = bundle1.getParcelableArrayList("stepsList");
bakingItem.servings = in.readInt();
return bakingItem;
}
@Override
public BakingItem[] newArray(int i) {
return new BakingItem[i];
}
};
public int getId() {
return id;
}
public String getName() {
return name;
}
public ArrayList<StepsItem> getSteps() {
return steps;
}
public int getServings() {
return servings;
}
public static class StepsItem implements Parcelable {
private int idSteps;
private String shortDescription;
@Override
public void writeToParcel(Parcel out, int flags) {
out.writeInt(idSteps);
out.writeString(shortDescription);
}
protected StepsItem(Parcel in) {
this.idSteps = in.readInt();
this.shortDescription = in.readString();
}
public StepsItem() {
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<StepsItem> CREATOR = new Creator<StepsItem>() {
@Override
public StepsItem createFromParcel(Parcel in) {
return new StepsItem(in);
}
@Override
public StepsItem[] newArray(int i) {
return new StepsItem[i];
}
};
public int getId() {
return idSteps;
}
public String getShortDescription() {
return shortDescription;
}
}
}
what the problem with parcelable class ?