Android, How to use readTypedList method correctly in a Parcelable class?

烈酒焚心 提交于 2019-11-28 18:38:00

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:

private Chapitre(){
    listVideo = new ArrayList<Video>();
}

private Chapitre(Parcel source) {
    // Call the above constructor
    this();

    numero = source.readInt();
    titre = source.readString();
    description = source.readString();
    nbVideo = source.readInt();
    source.readTypedList(listeVideo, Video.CREATOR);
}

Another solution: use createTypedArrayList instead of readTypedList which requires a non-null List object reference

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.

Mike Jancola

Also consider initializing the object with an immutable empty list.

private ArrayList<Video> listevideo = Collections.emptyList()

More details here: Collections.emptyList() vs. new instance

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!