java - Android, How to use readTypedList method correctly in a Parcelable class? -
i have 2 activities, in first one, instanciate arraylist of object myobject. in second activity, need arraylist. i've found tutorial : http://prasanta-paul.blogspot.com/2010/06/android-parcelable-example.html have implemented class liked it's explain.
public class chapitre implements parcelable{
private int numero; private string titre; private string description; private int nbvideo; private arraylist<video> listevideo; public chapitre(int numero, string titre, string description, arraylist<video> listevideo) { this.numero = numero; this.titre = titre; this.description = description; this.listevideo = listevideo; this.nbvideo = listevideo.size(); } //getters , setters ... private chapitre(parcel source) { numero = source.readint(); titre = source.readstring(); description = source.readstring(); nbvideo = source.readint(); source.readtypedlist(listevideo, video.creator); } @override public int describecontents() { return 0; } @override public void writetoparcel(parcel dest, int flags) { dest.writeint(numero); dest.writestring(titre); dest.writestring(description); dest.writeint(nbvideo); dest.writetypedlist(listevideo); } public static final parcelable.creator creator = new parcelable.creator() { public chapitre createfromparcel(parcel in) { return new chapitre(in); } public chapitre[] newarray(int size) { return new chapitre[size]; } };
}
public class video implements parcelable{
private string titre; private int numero; private string url; private string description; private string imageurl; private bitmap image; private string duree; /** * * @param nom * @param numero * @param url * @param description */ public video(string titre, string url, string description) { super(); this.titre = titre; this.url = url; this.description = description; } public video(int numero, string titre, string url, string description) { super(); this.titre = titre; this.url = url; this.description = description; this.numero = numero; } public video(string titre,int numero, string url, string description, string imageurl) { super(); this.titre = titre; this.url = url; this.description = description; this.imageurl = imageurl; this.numero = numero; setimage(fetchimage(imageurl)); } //getters , setters ... @override public int describecontents() { // todo auto-generated method stub return 0; } @override public void writetoparcel(parcel dest, int flags) { dest.writestring(titre); dest.writeint(numero); dest.writestring(url); dest.writestring(description); dest.writestring(imageurl); dest.writestring(duree); } public video(parcel source){ /* * reconstruct parcel */ log.v("tag", "parceldata(parcel source): time put parcel data"); titre = source.readstring(); numero = source.readint(); url = source.readstring(); description = source.readstring(); imageurl = source.readstring(); duree = source.readstring(); } public static final parcelable.creator creator = new parcelable.creator() { public video createfromparcel(parcel in) { return new video(in); } public video[] newarray(int size) { return new video[size]; } };
}
but nullpointexception on line "source.readtypedlist(listevideo, video.creator);" in class chapitre.
07-21 10:07:28.212: error/androidruntime(682): fatal exception: main 07-21 10:07:28.212: error/androidruntime(682): java.lang.runtimeexception: unable start activity componentinfo{com.genicorp.video.proto/com.genicorp.video.proto.lecture}: java.lang.nullpointerexception 07-21 10:07:28.212: error/androidruntime(682): @ android.app.activitythread.performlaunchactivity(activitythread.java:1736) 07-21 10:07:28.212: error/androidruntime(682): @ android.app.activitythread.handlelaunchactivity(activitythread.java:1752) 07-21 10:07:28.212: error/androidruntime(682): @ android.app.activitythread.access$1500(activitythread.java:123) 07-21 10:07:28.212: error/androidruntime(682): @ android.app.activitythread$h.handlemessage(activitythread.java:993) 07-21 10:07:28.212: error/androidruntime(682): @ android.os.handler.dispatchmessage(handler.java:99) 07-21 10:07:28.212: error/androidruntime(682): @ android.os.looper.loop(looper.java:126) 07-21 10:07:28.212: error/androidruntime(682): @ android.app.activitythread.main(activitythread.java:3997) 07-21 10:07:28.212: error/androidruntime(682): @ java.lang.reflect.method.invokenative(native method) 07-21 10:07:28.212: error/androidruntime(682): @ java.lang.reflect.method.invoke(method.java:491) 07-21 10:07:28.212: error/androidruntime(682): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:841) 07-21 10:07:28.212: error/androidruntime(682): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:599) 07-21 10:07:28.212: error/androidruntime(682): @ dalvik.system.nativestart.main(native method) 07-21 10:07:28.212: error/androidruntime(682): caused by: java.lang.nullpointerexception 07-21 10:07:28.212: error/androidruntime(682): @ android.os.parcel.readtypedlist(parcel.java:1630) 07-21 10:07:28.212: error/androidruntime(682): @ com.genicorp.video.proto.chapitre.<init>(chapitre.java:70) 07-21 10:07:28.212: error/androidruntime(682): @ com.genicorp.video.proto.chapitre.<init>(chapitre.java:65) 07-21 10:07:28.212: error/androidruntime(682): @ com.genicorp.video.proto.chapitre$1.createfromparcel(chapitre.java:89) 07-21 10:07:28.212: error/androidruntime(682): @ com.genicorp.video.proto.chapitre$1.createfromparcel(chapitre.java:1) 07-21 10:07:28.212: error/androidruntime(682): @ android.os.parcel.readparcelable(parcel.java:1981) 07-21 10:07:28.212: error/androidruntime(682): @ android.os.parcel.readvalue(parcel.java:1846) 07-21 10:07:28.212: error/androidruntime(682): @ android.os.parcel.readlistinternal(parcel.java:2092) 07-21 10:07:28.212: error/androidruntime(682): @ android.os.parcel.readarraylist(parcel.java:1536) 07-21 10:07:28.212: error/androidruntime(682): @ android.os.parcel.readvalue(parcel.java:1867) 07-21 10:07:28.212: error/androidruntime(682): @ android.os.parcel.readmapinternal(parcel.java:2083) 07-21 10:07:28.212: error/androidruntime(682): @ android.os.bundle.unparcel(bundle.java:215) 07-21 10:07:28.212: error/androidruntime(682): @ android.os.bundle.getparcelablearraylist(bundle.java:1151) 07-21 10:07:28.212: error/androidruntime(682): @ android.content.intent.getparcelablearraylistextra(intent.java:3634) 07-21 10:07:28.212: error/androidruntime(682): @ com.genicorp.video.proto.lecture.oncreate(lecture.java:37) 07-21 10:07:28.212: error/androidruntime(682): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1048) 07-21 10:07:28.212: error/androidruntime(682): @ android.app.activitythread.performlaunchactivity(activitythread.java:1700) 07-21 10:07:28.212: error/androidruntime(682): ... 11 more
i've waste 1 day on this, if me, great,
thanks in advance
hopefully, you've figured out now. else stumbles upon one. nullpointerexception getting caused arraylist never being initialized.
this fix it:
private chapitre(){ listvideo = new arraylist<video>(); } private chapitre(parcel source) { // call above constructor this(); numero = source.readint(); titre = source.readstring(); description = source.readstring(); nbvideo = source.readint(); source.readtypedlist(listevideo, video.creator); }
Comments
Post a Comment