Parcelable的source.readList不能在List <string>上工作

时间:2016-09-09 17:27:02

标签: android parcelable

这一行被IDE标记为红色:

mBook.Color=source.readList(Color,Book.class.getClassLoader());

我也改为

mBook.Color=source.readList(Color,String.class.getClassLoader());

但仍然错了,我认为这是因为我的代码缺少了getter&amp; color 字段的setter,但在创建它们之后,它仍然是错误的。我确定这不是让这个领域变得可行的方法,但不知道正确的方法是否正确

Book.java:

public class Book implements Parcelable {
   private String bookName;
   private int publishTime;
   private static List<String> Color = Arrays.asList(new String[]{"red","blue"});

   public String getBookName() {
      return bookName;
   }

   public static List<String> getColor() {
      return Color;
   }

   public static void setColor(List<String> Color) {
      Book.Color = Color;
   }

   public void setBookName(String bookName) {
      this.bookName = bookName;

   }

   public int getPublishTime() {
      return publishTime;
   }
   public void setPublishTime(int publishTime) {
      this.publishTime = publishTime;
   }

   public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>() {
      public Book createFromParcel(Parcel source) {
         Book mBook = new Book();
         mBook.bookName = source.readString();
         mBook.publishTime = source.readInt();
         mBook.Color =source.readList(Color,Book.class.getClassLoader());
         return mBook;
      }
      public Book[] newArray(int size) {
         return new Book[size];
      }
   };
   @Override
   public int describeContents() {
      return 0;
   }
   @Override
   public void writeToParcel(Parcel parcel, int flags) {
      parcel.writeString(bookName);
      parcel.writeInt(publishTime);
      parcel.writeList(Color);
   }
}

1 个答案:

答案 0 :(得分:1)

问题是readList返回一个void,这可能是你的IDE抱怨的。解决方案如下:

 mBook.Color = null;
 source.readList(Color,Book.class.getClassLoader());

希望它有所帮助。