我正在尝试获取this json api的数据
第一个主要活动的每个食谱的名称都很好,但是当我单击项目以开始详细活动时,应用程序崩溃了,错误原因是:
java.lang.IllegalStateException:捆绑包的无效魔术数字:0x0
主要活动
@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);
}
主要片段包:
Bundle recipesBundle = new Bundle();
recipesBundle.putParcelableArrayList(ALL_RECIPES, recipes);
详细活动:
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();
}
}
详细片段:
@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);
我将配方的名称从主要传递到详细信息,并收到了很好的结果,但是当我将其他详细信息添加到可拆分类(如步骤数组)时,应用程序开始崩溃,这就是可拆分类:
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;
}
}
}
可拆分类有什么问题?