Custom class object implements precautions Parcelable or Serializable interface

    Within the Android application, we usually use for passing data Bundle, in addition to basic data types, class objects can also pass, and is required to achieve a Parcelable or Serializable class serialization interface.

Suppose a custom class Book:

1, to achieve Serializable interface, the runtime throws an exception:

02-27 10:45:58.779: E/AndroidRuntime(14811): Caused by: java.lang.RuntimeException: Parcelable encountered IOException writing serializable object
(name = com.example.testinter.MainActivity$Disk)
可能的原因:

(1) Class Book access in question, for example: a class is an internal class. At this time, it may be defined as static or Book Book defined as a separate class Book.java.

No presence of other implements Serializable class object (2) in the class Book. For example: Bitmap, see API implementation is found to be Parcelable interface at this time needs to be changed to achieve Book Parcelable interface. [Original reference http://www.cnblogs.com/mengyan1124/p/5765580.html ]

2, which is independent Book Review written Book.java a separate class, and implements Parcelable interface, this interface a little complicated, particularly note the following:

(1) define the static constant Parcelable.Creator <Book> CREATOR;

Sequential read (2) Attributes field is consistent;

(3) if the field is not written in the writing end of the sequence, must be read otherwise, all subsequent fields are in error when read. Thus, if the field is not a read, write or not want to write it on the end of the sequence. Recommendation: For security reasons, fields written are read, if not read, meaning that the field would not exist.

Finally, attach the modified Book.java

 1 package com.example.testinter;
 2 
 3 import android.graphics.Bitmap;
 4 import android.os.Parcel;
 5 import android.os.Parcelable;
 6 
 7 public class Book implements Parcelable{
 8 
 9     public int id;
10     
11     public int ISBN;
12     
13     public String des;
14     
15     public Bitmap bitmap;
16     
17     public Book(){}
18     
19     // reading order: the above mentioned id ---> Bitmap ---> des ---> ISBN
 20      // and if you have not written to the field at the end of the sequence, you must read, otherwise, all subsequent fields error reading will. 
21 is      public Book (Parcel Parcel) {
 22 is          // the TODO Auto-Generated constructor Stub 
23 is          ID = parcel.readInt ();
 24          Bitmap = parcel.readParcelable (Bitmap. class .getClassLoader ());     // Bitmap read 
25          des = parcel.readString ();    
 26 is          ISBN = parcel.readInt ();
 27      }
 28  
29      @Override
 30      public  int describeContents() {
31         // TODO Auto-generated method stub
32         return 0;
33     }
34 
35     //写入顺序:id--->bitmap--->des--->ISBN
36     @Override
37     public void writeToParcel(Parcel parcel, int flags) {
38         // TODO Auto-generated method stub
39         parcel.writeInt(id);
40         parcel.writeParcelable(bitmap, flags);        //Bitmap的写入
41         parcel.writeString(des);
42         parcel.writeInt(ISBN);
43     }
44     
45     public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>() {
46         
47         @Override
48         public Book[] newArray(int sizes) {
49             // TODO Auto-generated method stub
50             return new Book[sizes];
51         }
52         
53         @Override
54         public Book createFromParcel(Parcel parcel) {
55             // TODO Auto-generated method stub
56             return new Book(parcel);
57         }
58     }; 
59 }

 

Guess you like

Origin www.cnblogs.com/sparrowlhl/p/11242727.html