自定义类的可分类

时间:2017-07-20 22:19:20

标签: java android parcelable

我目前正在尝试将类事件的ArrayList传递给另一个活动,如下所示..events是Event类的ArrayList。

Intent i = new Intent(ViewEvents.this, UserFeed.class);
i.putParcelableArrayListExtra("events",events);
startActivity(i);

然后我按如下方式检索数据:

Intent i = getIntent();
ArrayList<Event> events = i.getParcelableArrayListExtra("events");

我的问题是我在偏移5788处得到一个Umarshing未知类型代码32.我不太确定如何解决这个或为什么会发生这种情况。我已经在下面发布了所有三个类的所有代码。非常感谢您解决此问题。

public class Event implements Parcelable {
public String tvEventName;
public String tvEventInfo;
public String tvDescription;
public String ivEventImage;
public String organizerName;
public String eventId;
public String veneuId;
public String organizerId;
public Venue venue;
public Organizer organizer;


public Event(String tvEventName, String tvEventInfo, String tvDescription, String ivEventImage, String eventId, String veneuId,Venue venue,Organizer organizer) {
    this.tvEventName = tvEventName;
    this.tvEventInfo = tvEventInfo;
    this.tvDescription = tvDescription;
    this.ivEventImage = ivEventImage;
    this.eventId = eventId;
    this.veneuId = veneuId;
}

public Event() {
}

protected Event(Parcel in) {
    tvEventName = in.readString();
    tvEventInfo = in.readString();
    tvDescription = in.readString();
    ivEventImage = in.readString();
    organizerName = in.readString();
    eventId = in.readString();
    veneuId = in.readString();
    organizerId = in.readString();
}

public static final Creator<Event> CREATOR = new Creator<Event>() {
    @Override
    public Event createFromParcel(Parcel in) {
        return new Event(in);
    }

    @Override
    public Event[] newArray(int size) {
        return new Event[size];
    }
};

public static Event fromJSON(JSONObject jsonObject)throws JSONException {
    Event event = new Event();
    //Getting the name of the event
    JSONObject nameEvent = jsonObject.getJSONObject("name");
    event.tvEventName = nameEvent.getString("text");
    //Getting the description for the event Time and location only
    JSONObject eventInfo = jsonObject.getJSONObject("start");
    event.tvEventInfo = eventInfo.getString("utc");
    SimpleDateFormat existingUTCFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");
    SimpleDateFormat requiredFormat = new SimpleDateFormat("MM-dd hh:mm a");
    event.eventId = jsonObject.getString("id");
    try{
        Date getDate = existingUTCFormat.parse(event.tvEventInfo);
        String mydate = requiredFormat.format(getDate);
        event.tvEventInfo = mydate;
    }
    catch(ParseException e){
        e.printStackTrace();
    }
    //Getting the description of the event
    JSONObject eventDescription = jsonObject.getJSONObject("description");
    event.tvDescription = eventDescription.getString("text");
    //Getting a thumbnail of the image for futer use.
    try{
        jsonObject.getJSONObject("logo");
        JSONObject logo = jsonObject.getJSONObject("logo");
        JSONObject original= logo.getJSONObject("original");
        event.ivEventImage = original.getString("url");
        Log.i("Ingo",event.ivEventImage);
    }
    catch (Exception exception){
        event.ivEventImage ="@drawable/tree";
    }
    event.veneuId = jsonObject.getString("venue_id");
    event.organizerId = jsonObject.getString("organizer_id");
    return event;
}

public Organizer getOrganizer() {
    return organizer;
}

public void setOrganizer(Organizer organizer) {
    this.organizer = organizer;
}

public String getOrganizerId() {
    return organizerId;
}

public void setOrganizerId(String organizerId) {
    this.organizerId = organizerId;
}

public String getTvEventName() {
    return tvEventName;
}

public void setTvEventName(String tvEventName) {
    this.tvEventName = tvEventName;
}

public String getTvEventInfo() {
    return tvEventInfo;
}

public void setTvEventInfo(String tvEventInfo) {
    this.tvEventInfo = tvEventInfo;
}

public Venue getVenue() {
    return venue;
}

public void setVenue(Venue venue) {
    this.venue = venue;
}

public String getTvDescription() {
    return tvDescription;
}

public void setTvDescription(String tvDescription) {
    this.tvDescription = tvDescription;
}

public String getIvEventImage() {
    return ivEventImage;
}

public void setIvEventImage(String ivEventImage) {
    this.ivEventImage = ivEventImage;
}

public String getVeneuId() {
    return veneuId;
}

public void setVeneuId(String veneuId) {
    this.veneuId = veneuId;
}

public String getOrganizerName() {
    return organizerName;
}

public String getEventId() {
    return eventId;
}

public void setEventId(String eventId) {
    this.eventId = eventId;
}

public void setOrganizerName(String organizerName) {
    this.organizerName = organizerName;
}

@Override
public int describeContents() {
    return 0;
}
@RequiresApi(api = Build.VERSION_CODES.M)
@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(tvEventName);
    dest.writeString(tvEventInfo);
    dest.writeString(tvDescription);
    dest.writeString(ivEventImage);
    dest.writeString(organizerName);
    dest.writeString(eventId);
    dest.writeString(veneuId);
    dest.writeString(organizerId);
    dest.writeParcelable( this.venue,flags);
    dest.writeParcelable(this.organizer,flags);
}
}

CLASS 2

public class Venue implements Parcelable {
public String address;
public String city;
public String region;
public String postalCode;
public String country;
public String latitude;
public String longitude;
public String simpleAddress;

public Venue() {
}

public Venue(String address, String city, String region, String postalCode, String country, String latitude, String longitude, String simpleAddress) {
    this.address = address;
    this.city = city;
    this.region = region;
    this.postalCode = postalCode;
    this.country = country;
    this.latitude = latitude;
    this.longitude = longitude;
    this.simpleAddress = simpleAddress;
}

protected Venue(Parcel in) {
    address = in.readString();
    city = in.readString();
    region = in.readString();
    postalCode = in.readString();
    country = in.readString();
    latitude = in.readString();
    longitude = in.readString();
    simpleAddress = in.readString();
}

public static final Creator<Venue> CREATOR = new Creator<Venue>() {
    @Override
    public Venue createFromParcel(Parcel in) {
        return new Venue(in);
    }

    @Override
    public Venue[] newArray(int size) {
        return new Venue[size];
    }
};

public static Venue fromJSON(JSONObject jsonObject)throws JSONException {
    Venue venue = new Venue();
    if(jsonObject.getString("address_1") == null){
        if(jsonObject.getString("address_2") == null)
            venue.address = "No Location Available";
        else
            venue.address = jsonObject.getString("address_2");
    }
    else
        venue.address = jsonObject.getString("address_1");
    venue.city = jsonObject.getString("city");
    venue.region = jsonObject.getString("region");
    venue.postalCode = jsonObject.getString("postal_code");
    venue.country = jsonObject.getString("country");
    venue.latitude = jsonObject.getString("latitude");
    venue.longitude = jsonObject.getString("longitude");
    venue.simpleAddress =venue.address +","+ venue.city +","+ venue.country;
    Log.i("SIMPLE", venue.simpleAddress);
    return venue;
}
public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public String getCity() {
    return city;
}

public void setCity(String city) {
    this.city = city;
}

public String getRegion() {
    return region;
}

public void setRegion(String region) {
    this.region = region;
}

public String getPostalCode() {
    return postalCode;
}

public void setPostalCode(String postalCode) {
    this.postalCode = postalCode;
}

public String getCountry() {
    return country;
}

public void setCountry(String country) {
    this.country = country;
}

public String getLatitude() {
    return latitude;
}

public void setLatitude(String latitude) {
    this.latitude = latitude;
}

public String getLongitude() {
    return longitude;
}

public void setLongitude(String longitude) {
    this.longitude = longitude;
}

public String getSimpleAddress() {
    return simpleAddress;
}

public void setSimpleAddress(String simpleAddress) {
    this.simpleAddress = simpleAddress;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(address);
    dest.writeString(city);
    dest.writeString(region);
    dest.writeString(postalCode);
    dest.writeString(country);
    dest.writeString(latitude);
    dest.writeString(longitude);
    dest.writeString(simpleAddress);
}
}

CLASS 3

public class Organizer implements Parcelable{
public String description;
public String organizerId;
public String numePastEvents;
public String numFutureEvents;
public String website;
public String facebookUsername;
public String twitter;
public String name;

public Organizer() {
}

protected Organizer(Parcel in) {
    description = in.readString();
    organizerId = in.readString();
    numePastEvents = in.readString();
    numFutureEvents = in.readString();
    website = in.readString();
    facebookUsername = in.readString();
    twitter = in.readString();
    name = in.readString();
}

public static final Creator<Organizer> CREATOR = new Creator<Organizer>() {
    @Override
    public Organizer createFromParcel(Parcel in) {
        return new Organizer(in);
    }

    @Override
    public Organizer[] newArray(int size) {
        return new Organizer[size];
    }
};

public static Organizer fromJson(JSONObject jsonObject){
    Organizer organizer = new Organizer();
    try {
        organizer.description = jsonObject.getJSONObject("description").getString("text");
    } catch (JSONException e) {
        organizer.description ="NA";
        e.printStackTrace();
    }
    try {
        organizer.organizerId = jsonObject.getString("id");
    } catch (JSONException e) {
        organizer.organizerId = "NA";
        e.printStackTrace();
    }
    try {
        organizer.numePastEvents =  jsonObject.getString("num_past_events");
    } catch (JSONException e) {
        organizer.numePastEvents = "Na";
        e.printStackTrace();
    }
    try {
        organizer.numFutureEvents = jsonObject.getString("num_future_events");
    } catch (JSONException e) {
        organizer.numFutureEvents = "NA";
        e.printStackTrace();
    }
    try {
        organizer.website = jsonObject.getString("website");
    } catch (JSONException e) {
        Log.i("Error",e.getMessage());
        organizer.website = "NA";
        e.printStackTrace();
    }
    try {
        organizer.facebookUsername = jsonObject.getString("facebook");
    } catch (JSONException e) {
        organizer.facebookUsername = "NA";
        e.printStackTrace();
    }
    try {
        organizer.name = jsonObject.getString("name");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    try {
        organizer.twitter = jsonObject.getString("twitter");
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return organizer;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getTwitter() {
    return twitter;
}

public void setTwitter(String twitter) {
    this.twitter = twitter;
}

public String getDescription() {
    return description;
}

public void setDescription(String description) {
    this.description = description;
}

public String getOrganizerId() {
    return organizerId;
}

public void setOrganizerId(String organizerId) {
    this.organizerId = organizerId;
}

public String getNumePastEvents() {
    return numePastEvents;
}

public void setNumePastEvents(String numePastEvents) {
    this.numePastEvents = numePastEvents;
}

public String getNumFutureEvents() {
    return numFutureEvents;
}

public void setNumFutureEvents(String numFutureEvents) {
    this.numFutureEvents = numFutureEvents;
}

public String getWebsite() {
    return website;
}

public void setWebsite(String website) {
    this.website = website;
}

public String getFacebookUsername() {
    return facebookUsername;
}

public void setFacebookUsername(String facebookUsername) {
    this.facebookUsername = facebookUsername;
}

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(description);
    dest.writeString(organizerId);
    dest.writeString(numePastEvents);
    dest.writeString(numFutureEvents);
    dest.writeString(website);
    dest.writeString(facebookUsername);
    dest.writeString(twitter);
    dest.writeString(name);
}

1 个答案:

答案 0 :(得分:2)

实现Parcelable接口时,绝对要求您的读写完全匹配。在您的情况下,这是您的Event(Parcel)构造函数和void writeToParcel()

但是,您的writeToParcel()实现包括两个构造函数未读取的写入。

dest.writeParcelable( this.venue,flags);
dest.writeParcelable(this.organizer,flags);

您必须删除这些或将匹配的读取添加到构造函数。

venue = in.readParcelable(Venue.class.getClassLoader());
organizer = in.readParcelable(Organizer.class.getClassLoader());