Android,如何在 Parcelable 类中正确使用 readTypedList 方法?
我有两个活动,在第一个活动中,我实例化了对象 myObject 的 ArrayList。在第二个活动中,我需要获取这个Arraylist。我找到了一个教程: http://prasanta-paul.blogspot.com/2010 /06/android-parcelable-example.html 我已经实施了我的课程,就像它的解释一样。
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 and 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 and 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 from the Parcel
*/
Log.v("TAG", "ParcelData(Parcel source): time to put back 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];
}
};
}
但我在“source.readTypedList(listeVideo, Video.CREATOR);”行上得到 nullPointException在 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 to start activity ComponentInfo{com.genicorp.video.proto/com.genicorp.video.proto.Lecture}: java.lang.NullPointerException
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.os.Handler.dispatchMessage(Handler.java:99)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.os.Looper.loop(Looper.java:126)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.app.ActivityThread.main(ActivityThread.java:3997)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at java.lang.reflect.Method.invokeNative(Native Method)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at java.lang.reflect.Method.invoke(Method.java:491)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at 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): at android.os.Parcel.readTypedList(Parcel.java:1630)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at com.genicorp.video.proto.Chapitre.<init>(Chapitre.java:70)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at com.genicorp.video.proto.Chapitre.<init>(Chapitre.java:65)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at com.genicorp.video.proto.Chapitre$1.createFromParcel(Chapitre.java:89)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at com.genicorp.video.proto.Chapitre$1.createFromParcel(Chapitre.java:1)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.os.Parcel.readParcelable(Parcel.java:1981)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.os.Parcel.readValue(Parcel.java:1846)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.os.Parcel.readListInternal(Parcel.java:2092)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.os.Parcel.readArrayList(Parcel.java:1536)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.os.Parcel.readValue(Parcel.java:1867)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.os.Parcel.readMapInternal(Parcel.java:2083)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.os.Bundle.unparcel(Bundle.java:215)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.os.Bundle.getParcelableArrayList(Bundle.java:1151)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.content.Intent.getParcelableArrayListExtra(Intent.java:3634)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at com.genicorp.video.proto.Lecture.onCreate(Lecture.java:37)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): ... 11 more
我已经在这上面浪费了 1 天,所以如果有人能帮助我,那就太好了,
提前致谢
I have two Activities, in the first one, I instanciate an ArrayList of Object myObject. In the second activity, i need to get this Arraylist. I've found a tutorial : http://prasanta-paul.blogspot.com/2010/06/android-parcelable-example.html
I have implemented my 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 and 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 and 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 from the Parcel
*/
Log.v("TAG", "ParcelData(Parcel source): time to put back 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 I get nullPointException on the line "source.readTypedList(listeVideo, Video.CREATOR);" in the 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 to start activity ComponentInfo{com.genicorp.video.proto/com.genicorp.video.proto.Lecture}: java.lang.NullPointerException
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.os.Handler.dispatchMessage(Handler.java:99)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.os.Looper.loop(Looper.java:126)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.app.ActivityThread.main(ActivityThread.java:3997)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at java.lang.reflect.Method.invokeNative(Native Method)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at java.lang.reflect.Method.invoke(Method.java:491)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at 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): at android.os.Parcel.readTypedList(Parcel.java:1630)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at com.genicorp.video.proto.Chapitre.<init>(Chapitre.java:70)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at com.genicorp.video.proto.Chapitre.<init>(Chapitre.java:65)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at com.genicorp.video.proto.Chapitre$1.createFromParcel(Chapitre.java:89)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at com.genicorp.video.proto.Chapitre$1.createFromParcel(Chapitre.java:1)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.os.Parcel.readParcelable(Parcel.java:1981)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.os.Parcel.readValue(Parcel.java:1846)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.os.Parcel.readListInternal(Parcel.java:2092)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.os.Parcel.readArrayList(Parcel.java:1536)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.os.Parcel.readValue(Parcel.java:1867)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.os.Parcel.readMapInternal(Parcel.java:2083)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.os.Bundle.unparcel(Bundle.java:215)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.os.Bundle.getParcelableArrayList(Bundle.java:1151)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.content.Intent.getParcelableArrayListExtra(Intent.java:3634)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at com.genicorp.video.proto.Lecture.onCreate(Lecture.java:37)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
07-21 10:07:28.212: ERROR/AndroidRuntime(682): ... 11 more
I've already waste 1 day on this, so if anyone could help me, it would be great,
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
希望您现在已经明白了这一点。但对于其他偶然发现这个的人来说。您收到的 NullPointerException 是由 ArrayList 从未初始化引起的。
这可以修复它:
Hopefully, you've figured this out by now. But for anyone else who stumbles upon this one. The NullPointerException you were getting was caused by the ArrayList never being initialized.
This would fix it:
另一个解决方案:使用
createTypedArrayList
而不是readTypedList
,后者需要一个非空 List 对象引用Another solution: use
createTypedArrayList
instead ofreadTypedList
which requires a non-null List object reference另请记住,您必须以相同的顺序读取和写入可分割对象属性!
我也花了2个小时,因为解组与编组的顺序不同。
Also keep in mind that you have to read and write parcelable object attribute in the same order!
I also 2 hours because the unmarshalling was not in the same order of the marshalling.
还可以考虑使用不可变的空列表来初始化对象。
私有ArrayList<视频> listevideo = Collections.emptyList()
更多详细信息:Collections.emptyList() 与新实例
Also consider initializing the object with an immutable empty list.
private ArrayList<Video> listevideo = Collections.emptyList()
More details here: Collections.emptyList() vs. new instance