我有 3 個課程MainActivity:homePage和createPage; 和一個串列List<Recipe> recipeList = new ArrayList<>()。MainActivity
用戶輸入homePagefrom MainActivity。從homePage,用戶可以輸入createPage和創建新配方。這個新配方旨在傳回給MainActivity.
我在網上搜索了一下,發現有包裹。但是當我嘗試時,我得到了 NullPointerException。
createPage串列傳遞位置的代碼
ArrayList<Recipe> rList = new ArrayList<>();
Recipe r = new Recipe(...);
rList.add(r)
Intent i = new Intent();
Bundle b = new Bundle();
b.putParcelableArrayList("recipe", (ArrayList<? extends Parcelable>) rList);
i.putExtras(b);
i.setClass(createPage.this, homePage.class);
startActivity(i);
homePage接收串列的位置的代碼。
有什么問題getIntent()嗎?因為從MainActivityto移動時homePage,它不會收到捆綁包。這是導致錯誤嗎?
Intent intent = getIntent();
Bundle b = this.getIntent().getExtras();
if (b != null) {
Recipe r = b.getParcelable("recipe");
recipeList.add(r);
}
Recipe類代碼
public class Recipe implements Parcelable {
private String name;
private String description;
private String ingredients;
private int duration;
private String steps;
private int thumbnail;
protected Recipe(Parcel in) {
name = in.readString();
description = in.readString();
ingredients = in.readString();
duration = in.readInt();
steps = in.readString();
thumbnail = in.readInt();
}
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];
}
};
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getIngredients() {
return ingredients;
}
public void setIngredients(String ingredients) {
this.ingredients = ingredients;
}
public int getDuration() {
return duration;
}
public void setDuration(int duration) {
this.duration = duration;
}
public String getSteps() { return steps; }
public void setSteps(String steps) { this.steps = steps; }
public int getThumbnail() { return thumbnail; }
public Recipe() {}
public Recipe(String name, int duration, String ingredients, String description, String steps, int thumbnail) {
this.name = name;
this.description = description;
this.ingredients = ingredients;
this.duration = duration;
this.steps = steps;
this.thumbnail = thumbnail;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int i) {
parcel.writeString(name);
parcel.writeString(description);
parcel.writeString(ingredients);
parcel.writeInt(duration);
parcel.writeString(steps);
parcel.writeInt(thumbnail);
}
}
uj5u.com熱心網友回復:
您正在鍵下寫入Parcelable整個陣列"recipe"
b.putParcelableArrayList("recipe", (ArrayList<? extends Parcelable>) rList);
但另一方面,您不是在尋找串列,而是Recipe在同一鍵下尋找單個專案
Recipe r = b.getParcelable("recipe");
你應該使用getParcelableArrayList,或者如果你只有一個Recipe用于傳遞給另一個Activity只是使用putParcelable(不列出)
uj5u.com熱心網友回復:
或者,您可以使用可序列化,這將不那么復雜。
供參考:https ://stackoverflow.com/a/2736612/9502601
盡管 Parcellables 更快,但如果您想要一個不太復雜的解決方案,那么您可以選擇它。
用于 Serializable 和 Parcelable 之間的比較。 https://stackoverflow.com/a/23647471/9502601
uj5u.com熱心網友回復:
您可以為此使用這個 gson Lib
implementation 'com.google.code.gson:gson:2.8.9'
有意圖地發送資料
Recipe r = new Recipe(...);
String recipeString = new Gson().toJson(r);
intent.putExtra("recipe",recipeString);
// For ArrayList
ArrayList<Recipe> recipeList = new ArrayList<>();
String recipeString = new Gson().toJson(recipeList);
intent.putExtra("recipeList",recipeString);
從 Intent 接收資料
Recipe r = new Gson().fromJson(intent.getStringExtra("recipe"), Recipe.class);
// For Array List
Type listType = new TypeToken<ArrayList<Recipe>>(){}.getType();
ArrayList<Recipe> recipeList = new Gson().fromJson(intent.getStringExtra("recipeList"),listType);
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/493446.html
