可分割对象中的 Arraylist

发布于 2024-10-12 03:12:30 字数 2071 浏览 3 评论 0原文

到目前为止,我已经看到了许多可分割的示例,但由于某种原因,当它变得更复杂时,我无法让它工作。 我有一个 Movie 对象,它实现了 Parcelable。该 book 对象包含一些属性,例如 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();
   }

}

如果有人能让我走上正轨,我将非常感激,我花了相当多的时间寻找这个!

提前致谢 韦斯利

I have seen many parcelable examples so far, but for some reason I can't get it to work when it gets a bit more complex.
I have a Movie object, which implements Parcelable. This book object contains some properties, such as ArrayLists.
Running my app results in a NullPointerException when executing the ReadTypedList ! I'm really out of ideas here

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

The Review class:

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

}

I would be very grateful if someone could just get me on the right track, I have spend quite a bit of time searching for this one !

Thanks in adnvance
Wesley

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

红玫瑰 2024-10-19 03:12:30

reviewsauthors 均为空。您应该首先初始化 ArrayList。一种方法是链接构造函数:

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

reviews and authors are both null. You should first initialize the ArrayList. One way to do this is chain the constructor:

public Movie (Parcel in) {
   this();
   readFromParcel(in); 
}
回忆躺在深渊里 2024-10-19 03:12:30

来自 readTypedList 的 javadoc:

读入包含特定对象类型的给定列表项
使用 writeTypedList(List)

编写的

在当前 dataPosition() 写入的。该列表之前必须已通过 writeTypedList(List) 使用相同的对象类型写入。

你用简单的方式写下它们

dest.writeList(reviews);

From the javadocs for readTypedList:

Read into the given List items containing a particular object type
that were written with writeTypedList(List)

at the current dataPosition(). The list must have previously been written via writeTypedList(List) with the same object type.

You wrote them with a plain

dest.writeList(reviews);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文