我有以下Recipe
类,它正在实现Parcelable
类。但是当我将对象从一个类传递到另一个类时,它的属性值为null
. 为什么?
配方类:
package mobile.bh.classes;
import java.util.ArrayList;
import mobile.bh.activities.MethodStep;
import android.graphics.Bitmap;
import android.os.Parcel;
import android.os.Parcelable;
//simple class that just has one member property as an example
public class Recipe implements Parcelable {
public int id;
public String name;
public ArrayList<Ingredient> ingredients;
public ArrayList<MethodStep> method;
public String comment;
public String image;
public Bitmap image2;
public Recipe(){}
/* everything below here is for implementing Parcelable */
// 99.9% of the time you can just ignore this
public int describeContents() {
return 0;
}
// write your object's data to the passed-in Parcel
public void writeToParcel(Parcel out, int flags) {
out.writeInt(id);
out.writeString(name);
out.writeList(ingredients);
out.writeList(method);
out.writeString(comment);
out.writeString(image);
}
// this is used to regenerate your object. All Parcelables must have a CREATOR that implements these two methods
public static final Parcelable.Creator<Recipe> CREATOR = new Parcelable.Creator<Recipe>() {
public Recipe createFromParcel(Parcel in) {
return new Recipe(in);
}
public Recipe[] newArray(int size) {
return new Recipe[size];
}
};
// example constructor that takes a Parcel and gives you an object populated with it's values
private Recipe(Parcel in) {
in.writeInt(id);
in.writeString(name);
in.writeList(ingredients);
in.writeList(method);
in.writeString(comment);
in.writeString(image);
}
}
虽然发送对象intent
Intent i = new Intent(context,RecipeInfoActivity.class);
i.putExtra("recipeObj", recipe);
在另一边接收物体
Recipe p = (Recipe) getIntent().getParcelableExtra("recipeObj");
但p.name
值为null