intent.getExtra()返回null对象

时间:2014-06-27 10:19:05

标签: android android-intent android-activity

我试图在我的android应用程序中的两个活动之间传递类对象,但我总是在其他活动上得到null。

这是我的代码:

传递数据:

public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    try {
        Products mCurrentProduct = (Products) adapterView.getAdapter().getItem(i);
        Intent mProductDescription = new Intent(getBaseContext(), Activity_ProductDescription.class);
        Bundle mBundle = new Bundle();
        mBundle.putParcelable(GlobalStrings.EXTRA_MESSAGE_DATA, mCurrentProduct);
        mProductDescription.putExtras(mBundle);

        if (mProductDescription != null)
            startActivity(mProductDescription);
    }
    catch (Exception e)
    {
        Log.d("Selection erro :",e.getMessage());
    }
}

获取数据:

  Intent mIntent =  getIntent();
        Bundle mBundleData = mIntent.getExtras();
        Products mCurrentProduct = (Products) mBundleData.getParcelable(EXTRA_MESSAGE_DATA);
        Products p = (Products) getIntent().getParcelableExtra(EXTRA_MESSAGE_DATA); // p is always null here.

我的parcable类:

    public class Products implements Parcelable {

    public String productName;
    public double productPrice;
    public String productSize;
    public String productWeight;
    public String productDescription;
    public String brandName;
    public byte[] productImage;
    public String seller;

    public Products(String productName, double productPrice, String productSize, String productWeight,
                    String productDescription, String brandName, byte[] productImage, String seller) {

        this.productName = productName;
        this.productPrice = productPrice;
        this.productSize = productSize;
        this.productWeight = productWeight;
        this.productDescription = productDescription;
        this.brandName = brandName;
        this.productImage = productImage;
        this.seller = seller;
    }

    public String getProductName() {
        return productName;
    }

    public double getProductPrice() {
        return productPrice;
    }

    public String getProductSize() {
        return productSize;
    }

    public String getProductWeight() {
        return productWeight;
    }

    public String getProductDescription() {
        return productDescription;
    }

    public String getBrandName() {
        return brandName;
    }

    public byte[] getProductImage() {
        return productImage;
    }

    public String getSeller() {
        return seller;
    }

    private Products(Parcel p) {
        this.productName = p.readString();
        this.productPrice = p.readDouble();
        this.productSize = p.readString();
        this.productWeight = p.readString();
        this.productDescription = p.readString();
        this.brandName = p.readString();
        p.readByteArray(productImage);
        this.seller = p.readString();
    }
    public static final Creator<Products> CREATOR = new Creator<Products>() {

        @Override
        public Products createFromParcel(Parcel parcel) {
            return new Products(parcel);
        }

        @Override
        public Products[] newArray(int i) {
            return new Products[i];
        }
    };
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(productName);
        parcel.writeString(productSize);
        parcel.writeString(productWeight);
        parcel.writeString(productDescription);
        parcel.writeString(brandName);
        parcel.writeString(seller);
        parcel.writeByteArray(productImage);
        parcel.writeDouble(productPrice);

    }
}

为listview准备适配器的代码:

JSONObject jsonObject = new JSONObject(s);
JSONArray jsonArray = jsonObject.getJSONArray("GetProductsResult");

for ( int i=0; i < jsonArray.length();i++  )
{
JSONObject jsonObjectArr = jsonArray.getJSONObject(i);

productsList.add(new Products(
        jsonObjectArr.getString("ProductName"),
        jsonObjectArr.getDouble("ProductPrice"),
        jsonObjectArr.getString("ProductSize"),
        jsonObjectArr.getString("ProductWeight"),
        jsonObjectArr.getString("ProductDescription"),
        jsonObjectArr.getString("BrandName"),
        jsonObjectArr.getString("ProductImage").getBytes(),
        jsonObjectArr.getString("Seller")));

}

ArrayAdapter<Products> adapter = new ProductListItemAdapters(this,R.layout.list_products,productsList);
mListViewProductList = (ListView) findViewById(R.id.listViewProductList);
mListViewProductList.setAdapter(adapter);
mListViewProductList.setOnItemClickListener(this);

适配器类

public class ProductListItemAdapters extends ArrayAdapter<Products> {

Context mainContext;
List<Products> productList;
int resourceId;

public ProductListItemAdapters(Context context, int resource, List<Products> objects) {
    super(context, resource, objects);
    mainContext = context;
    productList = objects;
    resourceId = resource;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    View itemView = convertView;
        try {

            if (itemView == null) {

                LayoutInflater inflater = (LayoutInflater) mainContext
                        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                itemView = inflater.inflate(R.layout.list_products, parent, false);
            }

            Products prod = productList.get(position);

            TextView txtViewSeller = (TextView) itemView.findViewById(R.id.textView_productSeller);
            txtViewSeller.setText(prod.getSeller());

            TextView txtViewBrand = (TextView) itemView.findViewById(R.id.textView_productBrand);
            txtViewBrand.setText(prod.getBrandName());

            TextView txtViewProduct = (TextView) itemView.findViewById(R.id.textView_productName);
            txtViewProduct.setText(prod.getProductName());

            TextView txtViewDesc = (TextView) itemView.findViewById(R.id.textView_productDesc);
            txtViewDesc.setText(prod.getProductDescription());

            TextView txtViewPrice = (TextView) itemView.findViewById(R.id.textView_productPrice);
            txtViewPrice.setText(String.valueOf(prod.getProductPrice()));

        }
        catch(Exception e) {
            Log.d("err", e.toString() );}

    return itemView;
}

}

我做错了吗?

2 个答案:

答案 0 :(得分:1)

不是回答你的实际问题,但是你做错了。您正在写入包裹的字段序列在阅读时必须相同。

喜欢

// You followed this sequesnce for writting
parcel.writeString(productName);
    parcel.writeString(productSize);
    parcel.writeString(productWeight);
    parcel.writeString(productDescription);
    parcel.writeString(brandName);
    parcel.writeString(seller);
    parcel.writeByteArray(productImage);
    parcel.writeDouble(productPrice);

但在阅读时

this.productName = p.readString();
    this.productPrice = p.readDouble();
    this.productSize = p.readString();

与序列不匹配。

答案 1 :(得分:1)

根据Android推荐,我将始终建议使用Parcelable

ParcelParcelable很快,但其文档说明您不得将其用于存储的通用序列化,因为实施因Android的不同版本而异(即操作系统更新可能会中断)一个依赖它的应用程序)

简单的实现Serializable接口。它简单易用。 类似的东西:

public class Products implements Serializable{
   //write all your getter/setter methods here
}

以简单的putExtra()方法发送对象,并将其作为

getIntent().getSerializableExtra("Key");