0

This line is red flagged by IDE :

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

I also changed to

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

but still wrong, I thought it's because my code was missing getter & setter for color field, but after creating them, it's still wrong. I'm sure this is not the way to make the field parceabled but don't know either the correct way to make correct

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);
   }
}
  • FYI, conventionally your variable names should all start with lowercase. As it is, `Color` is confusing since it appears to be a class, rather than a variable. (you can see the difference in the way the syntax highlighter formats it) – Kevin Coppock Sep 09 '16 at 18:21

1 Answers1

1

The problem is that readList returns a void so that might be what your IDE is complaining about. The solution is the following :

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

Hope it helps.

BOUTERBIAT Oualid
  • 1,494
  • 13
  • 15
  • Ah I missed that `source.readList()` is actually *void* because getting used to *non void* `readString()`, `getInt()`, etc. Thanks! – Plain_Dude_Sleeping_Alone Sep 09 '16 at 17:47
  • Alternatively you might want to use `createTypedArrayList(Book.CREATOR)`. Using `readList()` requires that the list that you pass in be the correct size of the parceled list (i.e. you'll need to store the list size as a separate parceled value). `createTypedArrayList(Book.CREATOR)` will return a list you can use to assign the value (i.e. `mBook.color = source.createTypedArrayList(Book.CREATOR)`) – Kevin Coppock Sep 09 '16 at 18:18
  • @kcoppock, technically this answer isn't correct either, I just accepted it as it pointed me out that I simply assigned void method which is not returning a value (I found it funny since `readInt()` is non void, but `readList()` is). The wrong part is that `Book.class.getClassLoader()` gives wrong run time type as should be `String.class.getClassLoader()`. I also avoided `createTypedArrayList` as the list's signature type is simply String and not a custome class. Ah android is confusing anyway, let's shopping!. – Plain_Dude_Sleeping_Alone Sep 10 '16 at 06:12