java.util.ArrayList cannot be cast to android.os.Parcelable. key expected Parcelable but value was a java.util.ArrayList -
what reason of below error
key object_list expected parcelable value java.util.arraylist. default value <null> returned.
this full code
public class singleobjectactivity extends activity { public static final string object_list = "object_list"; private arraylist<listitem> objects; public imageview imgview; private listitem listobjects; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.singleobject); bundle extras = getintent().getextras(); imgview = (imageview) findviewbyid(r.id.funnyimage); if (extras.containskey(object_list)) { this.listobjects = extras.getparcelable(object_list); } else { this.listobjects = null; } if (listobjects != null) { picasso. with(getapplicationcontext()). load(listobjects.geturl()) //load() .placeholder(r.drawable.logo) .fit() .nofade() .into(imgview); } } }
sending intent
arraylist<listitem> personarraylist = new arraylist<>(); personarraylist.add(new listitem(item.getorder(), item.getid(), item.geturl(), item.getusername(), item.getlikes())); intent intent = new intent(mcontext,singleobjectactivity.class); intent.putextra("object_list", personarraylist); intent.addflags(intent.flag_activity_new_task); mcontext.startactivity(intent);
edit 4
arraylist<listitem> items = getintent().getparcelablearraylistextra("object_list"); bundle extras = getintent().getextras(); imgview = (imageview) findviewbyid(r.id.funnyimage); /* if (extras != null) { objects = extras.getparcelablearraylist(object_list); } else { objects = new arraylist<listitem>(); }*/ if (extras.containskey(object_list)) { // this.listobjects = extras.getparcelable(object_list); // this.listobjects = (listitem) extras.getparcelable(object_list); this.objects= extras.getparcelablearraylist("object_list"); } else { this.objects = null; } if (this.listobjects != null) { picasso. with(getapplicationcontext()). load(this.listobjects.geturt()) //load() .placeholder(r.drawable.logo) .fit() .nofade() .into(imgview); } }
when sending arraylist
:
intent intent = new intent(context ,targetactivity.class); intent.putparcelablearraylistextra("object_list", personarraylist); context.startactivity(intent);
when receiving arraylist
:
arraylist<listitem> items = getintent().getparcelablearraylistextra("object_list");
note not same getintent().getextras().getparcelablearraylist()
. currently, believe incorrectly writing
bundle extras = geintent().getextras(); extras.getparcelablearraylist("object_list");
also, listitem
, despite implementing parcelable
, not copy of values. isn't causing issue worth keeping in mind.
Comments
Post a Comment