intent.getExtra()返回空对象 [英] intent.getExtra() returns null object

查看:145
本文介绍了intent.getExtra()返回空对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我的代码:

传递数据:

I am trying to pass class object between two activities in my android application, but I always get null on the other activity.

here is my code :

To pass data :

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());
        }
    }




获取数据:




To get data :

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




我的寓言课:




my parcable class:

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);

       }
   }




代码以为列表视图准备适配器:




code to prepare adapter for 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);



适配器类



Adapter Class

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;
    }
}





我做错什么了吗?





Am I doing anything wrong ?

推荐答案

您确定p中存在问题吗?
Are you sure that you have problem in p?
Products p = (Products) getIntent().getParcelableExtra(EXTRA_MESSAGE_DATA); 


为什么在获取mCurrentProduct的同时又尝试获取相同的详细信息?


Why are trying to get same detail again, while you getting mCurrentProduct?

Products mCurrentProduct = (Products) mBundleData.getParcelable(EXTRA_MESSAGE_DATA);


这篇关于intent.getExtra()返回空对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆