Android Parcelable对象返回空值

时间:2017-01-05 01:50:24

标签: android

我正在使用parcelable将类传递给新活动。但是,在接收方,我得到空值。很奇怪,我得到空值而不是空值。但是,我确实希望每个属性都有一些值。我已检查调用者部分的属性是否为空。为什么会这样?

这是我的实施:

致电新活动

Intent intent = new Intent(ProductDetailsActivity.this, ShopActivity.class);
intent.putExtra(ShopActivity.KEY_COMPANY, products.get(currentTypePosition).companyDetails);
startActivity(intent);

接收

Company company = getIntent().getParcelableExtra(KEY_COMPANY);

Parcelable

public class Company implements Parcelable {
    public static final Parcelable.Creator CREATOR = new Parcelable.Creator() {
        public Company createFromParcel(Parcel in) {
            return new Company(in);
        }

        public Company[] newArray(int size) {
            return new Company[size];
        }
    };

    // TODO: Remove default values once backend is completed
    public ArrayList<CompanyProduct> companyProducts = getDefaultCompanyProducts();
    public String name = "hello";
    public int icon = 0;
    public int productCount = 100;
    public int followerCount = 9000;
    public String location = "hello";
    public String contact = "400-23729";
    public String description = "hello";
    public int storeCount = 100;

    public Company() {
        super();
    }

    public Company(Parcel in) {
        in.readTypedList(companyProducts, Status.CREATOR);
        name = in.readString();
        icon= in.readInt();
        productCount = in.readInt();
        followerCount = in.readInt();
        location = in.readString();
        contact = in.readString();
        description = in.readString();
        storeCount = in.readInt();
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeTypedList(companyProducts);
        dest.writeString(name);
        dest.writeInt(icon);
        dest.writeInt(productCount);
        dest.writeInt(followerCount);
        dest.writeString(location);
        dest.writeString(contact);
        dest.writeString(description);
        dest.writeInt(storeCount);
    }
}

0 个答案:

没有答案