studio putparcelable online example java android arraylist parcelable typed

java - putparcelable - Arraylist en objeto parcelable



parcelable android studio (2)

De los javadocs para readTypedList :

Lea en los elementos de Lista dados que contienen un tipo de objeto particular que se escribieron con writeTypedList(List)

en la posición de datos actual dataPosition() . La lista debe haberse escrito previamente mediante writeTypedList(List) con el mismo tipo de objeto.

Usted los escribió con un simple

dest.writeList(reviews);

He visto muchos ejemplos de parcelable hasta ahora, pero por alguna razón no puedo hacer que funcione cuando se vuelve un poco más complejo. Tengo un objeto Movie, que implementa Parcelable. Este libro contiene algunas propiedades, como ArrayLists. ¡Ejecutar los resultados de mi aplicación en una NullPointerException al ejecutar ReadTypedList! Realmente estoy sin ideas aquí

public class Movie implements Parcelable{ private int id; private List<Review> reviews private List<String> authors; public Movie () { reviews = new ArrayList<Review>(); authors = new ArrayList<String>(); } public Movie (Parcel in) { readFromParcel(in); } /* getters and setters excluded from code here */ public void writeToParcel(Parcel dest, int flags) { dest.writeInt(id); dest.writeList(reviews); dest.writeStringList(authors); } public static final Parcelable.Creator<Movie> CREATOR = new Parcelable.Creator<Movie>() { public MoviecreateFromParcel(Parcel source) { return new Movie(source); } public Movie[] newArray(int size) { return new Movie[size]; } }; /* * Constructor calls read to create object */ private void readFromParcel(Parcel in) { this.id = in.readInt(); in.readTypedList(reviews, Review.CREATOR); /* NULLPOINTER HERE */ in.readStringList(authors); } }

La clase de Revisión:

public class Review implements Parcelable { private int id; private String content; public Review() { } public Review(Parcel in) { readFromParcel(in); } public void writeToParcel(Parcel dest, int flags) { dest.writeInt(id); dest.writeString(content); } public static final Creator<Review> CREATOR = new Creator<Review>() { public Review createFromParcel(Parcel source) { return new Review(source); } public Review[] newArray(int size) { return new Review[size]; } }; private void readFromParcel(Parcel in) { this.id = in.readInt(); this.content = in.readString(); } }

Estaría muy agradecido si alguien pudiera ponerme en el camino correcto, ¡he gastado bastante tiempo buscando este!

Gracias por adelantado Wesley


reviews y los authors son nulos. Primero debe inicializar ArrayList. Una forma de hacerlo es encadenando el constructor:

public Movie (Parcel in) { this(); readFromParcel(in); }