android - intent.getExtra() returns null object -


i trying pass class object between 2 activities in android application, null on other activity.

here 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 data :

  intent mintent =  getintent();         bundle mbundledata = mintent.getextras();         products mcurrentproduct = (products) mbundledata.getparcelable(extra_message_data);         products p = (products) getintent().getparcelableextra(extra_message_data); // p 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 prepare adapter listview:

jsonobject jsonobject = new jsonobject(s); jsonarray jsonarray = jsonobject.getjsonarray("getproductsresult");  ( int i=0; < 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 doing wrong ?

as per android recomendation suggest use parcelable.

parcel , parcelable quick, documentation says must not use general-purpose serialization storage, since implementation varies different versions of android (i.e. os update break app relied on it)

simple implement serializable interface. simple , easy use. like:

public class products implements serializable{    //write getter/setter methods here } 

and send object in simple putextra() method of intent ,

getintent().getserializableextra("key"); 

Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -