3

我有以下類實現可分段的接口。在活動之間傳遞parcelable對象

用戶

public class User implements Parcelable { 

    public long id; 
    public String username; 
    public String email; 

    public String firstName; 
    public String description; 
    public String city; 

    public int numBeezs; 
    public int numLikes; 

    public String avatar; 
    public String background; 

    public String token; 

    public User() {} 

    public User(Parcel parcel) { 
      id = parcel.readInt(); 
      username = parcel.readString(); 
      email = parcel.readString(); 

      firstName = parcel.readString(); 
      description = parcel.readString(); 
      city = parcel.readString(); 

      numBeezs = parcel.readInt(); 
      numLikes = parcel.readInt(); 

      avatar = parcel.readString(); 
      background = parcel.readString(); 

      token = parcel.readString(); 
    } 

    public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() { 
      public User createFromParcel(Parcel in) { 
        return new User(in); 
      } 

      public User[] newArray(int size) { 
        return new User[size]; 
      } 
    }; 

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
      dest.writeLong(id); 
      dest.writeString(username); 
      dest.writeString(email); 

      dest.writeString(firstName); 
      dest.writeString(description); 
      dest.writeString(city); 

      dest.writeInt(numBeezs); 
      dest.writeInt(numLikes); 

      dest.writeString(avatar); 
      dest.writeString(background); 

      dest.writeString(token); 
    } 

    @Override 
    public int describeContents() { 
      return 0; 
    } 

} 

殖民地

public class Colony implements Parcelable { 

    public long id; 
    public String name; 
    public String image; 

    public int numBeezs; 
    public int numFollowers; 

    public boolean following; 

    public Colony (Parcel parcel) { 
      id = parcel.readInt(); 
      name = parcel.readString(); 
      image = parcel.readString(); 

      numBeezs = parcel.readInt(); 
      numFollowers = parcel.readInt(); 

      following = parcel.readInt() == 1; 
    } 

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
      dest.writeLong(id); 
      dest.writeString(name); 
      dest.writeString(image); 

      dest.writeInt(numBeezs); 
      dest.writeInt(numFollowers); 

      dest.writeInt(following ? 1 : 0); 
    } 


    public static final Parcelable.Creator<Colony> CREATOR = new Parcelable.Creator<Colony>() { 

      public Colony createFromParcel(Parcel in) { 
        return new Colony(in); 
      } 

      public Colony[] newArray(int size) { 
        return new Colony[size]; 
      } 
    }; 

    @Override 
    public int describeContents() { 
      return 1; 
    } 
} 

,最後一個

Beez

public class Beez implements Parcelable { 
    public Beez(Parcel parcel) { 
      id = parcel.readLong(); 
      text = parcel.readString(); 
      media = parcel.readString(); 

      user = parcel.readParcelable(User.class.getClassLoader()); 
      numLikes = parcel.readInt(); 
      numReplies = parcel.readInt(); 

      address = parcel.readParcelable(Address.class.getClassLoader()); 

      created = new Date(parcel.readLong()); 
      updated = new Date(parcel.readLong()); 

      colonies = new ArrayList<Colony>(); 
      parcel.readTypedList(colonies, Colony.CREATOR); 
    } 

    @Override 
    public int describeContents() { 
      return 2; 
    } 

    @Override 
    public void writeToParcel(Parcel dest, int flags) { 
      dest.writeLong(id); 
      dest.writeString(text); 
      dest.writeString(media); 

      dest.writeParcelable(user, user.describeContents()); 
      dest.writeInt(numLikes); 
      dest.writeInt(numReplies); 

      dest.writeParcelable(address, address.describeContents()); 

      dest.writeLong(created.getTime()); 
      dest.writeLong(updated.getTime()); 

      dest.writeTypedList(colonies); 
    } 

    public static final Parcelable.Creator<Beez> CREATOR = new Parcelable.Creator<Beez>() { 

      public Beez createFromParcel(Parcel in) { 
        return new Beez(in); 
      } 

      public Beez[] newArray(int size) { 
        return new Beez[size]; 
      } 
    }; 
} 

然後,當我開始從我的ArrayAdapter活動:

// prepare intent 
Intent intent = new Intent(activity, ActivityBeez.class); 

// add beez to intent 
Beez beez = getItem(position); 
intent.putExtra(ActivityBeez.EXTRA_BEEZ, beez); 

// start activity 
activity.startActivity(intent); 

日誌貓的報恩:

02-20 18:48:48.000: E/Parcel(11598): Class not found when unmarshalling: Álvaro iOS dev at Beezer Palma /media/usr/original/197.jpg /media/bgr/original/197.jpg, e: java.lang.ClassNotFoundException: Álvaro iOS dev at Beezer Palma /media/usr/original/197.jpg /media/bgr/original/197.jpg 

我在做什麼錯?我跟着this guide來實現可見界面。

+0

這是否真的與您的Parcelable實現有關?你能否通過評論putExtra及其用途來檢查。 – Prakash

+0

這個問題可能與putExtra有關。試試這個http://shri.blog.kraya.co.uk/2010/04/26/android-parcel-data-to-pass-between-activities-using-parcelable-classes/ – AndyFaizan

回答

1

我正在從包裹中讀取一個int(在Colony和User中),我正在寫一個long。

public Colony (Parcel parcel) { 
     id = parcel.readInt(); 

public void writeToParcel(Parcel dest, int flags) { 
     dest.writeLong(id); 
相關問題