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

最后一个

比兹

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

我究竟做错了什么?我按照这个指南来实现 parcelable 接口。

4

1 回答 1

1

我正在从包裹中读取一个 int(在 Colony 和 User 中),我正在写一个很长的内容。

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

public void writeToParcel(Parcel dest, int flags) {
        dest.writeLong(id);
于 2014-02-25T10:15:18.763 回答