2011-01-16 29 views
33

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! Estoy muy fuera de las ideas aquíArraylist en objeto parcelable

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 la opinió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 podría conseguirme en el camino correcto, tengo que pasar un poco de tiempo buscando este!

Gracias de adnvance Wesley

Respuesta

37

reviews y authors son tanto nulo. Primero debe inicializar ArrayList. Una forma de hacerlo es la cadena del constructor:

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

Muchas gracias MUUUUUCH, perdí 2 días en este :-). – Wesley

14

De los javadocs para readTypedList:

Leer en los elementos de la lista dada que contiene un objeto determinado tipo que fueron escritos con writeTypedList(List)

en el actual dataPosition(). La lista debe haberse escrito previamente a través del writeTypedList(List) con el mismo tipo de objeto.

Se escribía con una llanura

dest.writeList(reviews); 
+0

Vaya, lo cambié justo antes de copiar pegando mi código aquí. En mi proyecto tengo esto como escrito a máquina, ¡gracias por notarlo! – Wesley

+0

¡Muchas gracias! Me salvó horas y horas – CBaker

+0

Tienes razón. Muchas gracias. –

Cuestiones relacionadas