android - Passing an Array of object from activity to another generates me an error -
i want pass array of objects mainactivity
thelist
activity, generates me exception , application crashes. tried pass string array , worked perfectly. problem when try sent mainactivity
array of centreactivity
.centreactivity
class serializable
(it implements interface). code in mainactivity
:
for (int = 0; < array.length(); i++) { jsonobject row = array.getjsonobject(i); id[i] = row.getint("id"); name[i] = row.getstring("identifiant"); // activities[i] = id[i] + " " + name[i]; //that testing. centreactivity act = new centreactivity(id[i], name[i]); listactivitycentre[i] = act; } intent = new intent(mainactivity.this, thelist.class); i.putextra("activities", listactivitycentre); // here problem startactivity(i);
and here code of thelist
activity :
private centreactivity listeactivities[] = new centreactivity[2]; @suppresswarnings("unchecked") @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); listeactivities = (centreactivity[]) getintent().getserializableextra("activities"); (int i=0; i<listeactivities.length; i++){ int id = listeactivities[i].getid(); string iden = listeactivities[i].getidentifiant(); activities[i] = id + " " + iden; } setlistadapter(new arrayadapter(this, android.r.layout.simple_list_item_1, activities)); }
logcat :
05-14 01:43:28.432: e/androidruntime(3440): fatal exception: main 05-14 01:43:28.432: e/androidruntime(3440): java.lang.runtimeexception: unable start activity componentinfo{com.example.jsonparameters/com.example.jsonparameters.thelist}: java.lang.classcastexception: java.lang.object[] cannot cast com.example.jsonparameters.centreactivity[] 05-14 01:43:28.432: e/androidruntime(3440): @ android.app.activitythread.performlaunchactivity(activitythread.java:2180) 05-14 01:43:28.432: e/androidruntime(3440): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 05-14 01:43:28.432: e/androidruntime(3440): @ android.app.activitythread.access$600(activitythread.java:141) 05-14 01:43:28.432: e/androidruntime(3440): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 05-14 01:43:28.432: e/androidruntime(3440): @ android.os.handler.dispatchmessage(handler.java:99) 05-14 01:43:28.432: e/androidruntime(3440): @ android.os.looper.loop(looper.java:137) 05-14 01:43:28.432: e/androidruntime(3440): @ android.app.activitythread.main(activitythread.java:5039) 05-14 01:43:28.432: e/androidruntime(3440): @ java.lang.reflect.method.invokenative(native method) 05-14 01:43:28.432: e/androidruntime(3440): @ java.lang.reflect.method.invoke(method.java:511) 05-14 01:43:28.432: e/androidruntime(3440): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 05-14 01:43:28.432: e/androidruntime(3440): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 05-14 01:43:28.432: e/androidruntime(3440): @ dalvik.system.nativestart.main(native method) 05-14 01:43:28.432: e/androidruntime(3440): caused by: java.lang.classcastexception: java.lang.object[] cannot cast com.example.jsonparameters.centreactivity[] 05-14 01:43:28.432: e/androidruntime(3440): @ com.example.jsonparameters.thelist.oncreate(thelist.java:18) 05-14 01:43:28.432: e/androidruntime(3440): @ android.app.activity.performcreate(activity.java:5104) 05-14 01:43:28.432: e/androidruntime(3440): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 05-14 01:43:28.432: e/androidruntime(3440): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144)
thank me figuring out problem !
the exception coming because not not accessing correct data are putting previous activity. guess problem in listeactivities[]
check , fix problem , in case using parse able arrarylist
for sending
intent.putextra("leavedata", lsempleave);
here lsempleave object of arraylist
for getting
arraylist<abc> lsempleave1 = new arraylist<abc>(); bundle extras = getintent().getextras(); lsempleave1 = extras.getparcelablearraylist("leavedata");
Comments
Post a Comment