質問

私はこれまでに多くの小包の例を見てきましたが、何らかの理由で、もう少し複雑になったときに機能させることはできません。私は映画のオブジェクトを持っています。この本のオブジェクトには、ArrayListsなどのいくつかのプロパティが含まれています。私のアプリを実行すると、readtypedlistを実行するときにnullpointerexceptionが発生します!私はここで本当にアイデアから外れています

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);
   }
}

レビュークラス:

    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();
   }

}

誰かが私を正しい軌道に乗せることができれば、私はとても感謝しています、私はこれを探すのにかなりの時間を費やしました!

Adnvance Wesleyに感謝します

役に立ちましたか?

解決

reviewsauthors どちらもnullです。最初にArrayListを初期化する必要があります。これを行う1つの方法は、コンストラクターのチェーンです。

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

他のヒント

Javadocsから readTypedList:

書かれた特定のオブジェクトタイプを含む指定されたリスト項目を読み取ります writeTypedList(List)

電流で dataPosition(). 。リストは以前に書かれている必要があります writeTypedList(List) 同じオブジェクトタイプで。

あなたはそれらを平野で書いた

dest.writeList(reviews);
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top