【问题标题】:NULL value for attributes in Parcelable object. why?Parcelable 对象中属性的 NULL 值。为什么?
【发布时间】:2012-08-24 14:45:19
【问题描述】:

我有以下 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

【问题讨论】:

    标签: java android serialization android-intent parcelable


    【解决方案1】:

    在 Parcelable 构造函数中,您需要从包裹中回读。

    private Recipe(Parcel in) {
            id = in.readInt();
            name =in.readString();
            ingredients = in.readList();
            method = in.readList();
            comment = in.readString();
            }
    

    【讨论】:

      【解决方案2】:

      您应该在构造函数中使用 readInt 而不是 writeInt(其他字段等)

      【讨论】:

        【解决方案3】:

        首先,在您的构造函数中,您似乎试图将所有属性写入包裹,但据我所知,它们尚未设置;可能你打算从那里的包裹中阅读?现在我不确定这个 Parcel 到底是什么,但我在想它是某种类似于 Properties 的类?如果是这样,Java 就不是按引用传递的。这意味着仅修改传递给您的方法的值不会修改真实 Parcel 的值,您必须返回修改后的 Parcel

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-12-23
          • 2022-07-18
          • 1970-01-01
          • 2017-02-17
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2018-03-05
          相关资源
          最近更新 更多