我有一个RecyclerView
,并通过意图发送与项目相对应的对象,从而单击了要启动活动的任何项目。但是当我尝试发送对象时,应用程序崩溃了。
我的模特班
public class ProductListDataModel {
@SerializedName("id")
@Expose
private Integer id;
@SerializedName("product_id")
@Expose
private Integer productId;
@SerializedName("name")
@Expose
private String name;
@SerializedName("manufacturer")
@Expose
private Object manufacturer;
@SerializedName("sku")
@Expose
private String sku;
@SerializedName("model")
@Expose
private String model;
@SerializedName("image")
@Expose
private String image;
@SerializedName("images")
@Expose
private List<Object> images = null;
@SerializedName("original_image")
@Expose
private String originalImage;
@SerializedName("original_images")
@Expose
private List<Object> originalImages = null;
@SerializedName("price_excluding_tax")
@Expose
private Float priceExcludingTax;
@SerializedName("price")
@Expose
private Float price;
@SerializedName("price_formated")
@Expose
private String priceFormated;
@SerializedName("rating")
@Expose
private Integer rating;
@SerializedName("description")
@Expose
private String description;
@SerializedName("attribute_groups")
@Expose
private List<Object> attributeGroups = null;
@SerializedName("special")
@Expose
private Float special;
@SerializedName("special_excluding_tax")
@Expose
private Float specialExcludingTax;
@SerializedName("special_formated")
@Expose
private String specialFormated;
@SerializedName("special_start_date")
@Expose
private String specialStartDate;
@SerializedName("special_end_date")
@Expose
private String specialEndDate;
@SerializedName("discounts")
@Expose
private List<Discount> discounts = null;
@SerializedName("options")
@Expose
private List<Option> options = null;
@SerializedName("minimum")
@Expose
private String minimum;
@SerializedName("meta_title")
@Expose
private String metaTitle;
@SerializedName("meta_description")
@Expose
private String metaDescription;
@SerializedName("meta_keyword")
@Expose
private String metaKeyword;
@SerializedName("seo_url")
@Expose
private String seoUrl;
@SerializedName("tag")
@Expose
private String tag;
@SerializedName("upc")
@Expose
private String upc;
@SerializedName("ean")
@Expose
private String ean;
@SerializedName("jan")
@Expose
private String jan;
@SerializedName("isbn")
@Expose
private String isbn;
@SerializedName("mpn")
@Expose
private String mpn;
@SerializedName("location")
@Expose
private String location;
@SerializedName("stock_status")
@Expose
private String stockStatus;
@SerializedName("stock_status_id")
@Expose
private Integer stockStatusId;
@SerializedName("manufacturer_id")
@Expose
private Integer manufacturerId;
@SerializedName("tax_class_id")
@Expose
private Integer taxClassId;
@SerializedName("date_available")
@Expose
private String dateAvailable;
@SerializedName("weight")
@Expose
private String weight;
@SerializedName("weight_class_id")
@Expose
private Integer weightClassId;
@SerializedName("length")
@Expose
private String length;
@SerializedName("width")
@Expose
private String width;
@SerializedName("height")
@Expose
private String height;
@SerializedName("length_class_id")
@Expose
private Integer lengthClassId;
@SerializedName("subtract")
@Expose
private String subtract;
@SerializedName("sort_order")
@Expose
private String sortOrder;
@SerializedName("status")
@Expose
private String status;
@SerializedName("date_added")
@Expose
private String dateAdded;
@SerializedName("date_modified")
@Expose
private String dateModified;
@SerializedName("viewed")
@Expose
private String viewed;
@SerializedName("weight_class")
@Expose
private String weightClass;
@SerializedName("length_class")
@Expose
private String lengthClass;
@SerializedName("shipping")
@Expose
private String shipping;
@SerializedName("reward")
@Expose
private Object reward;
@SerializedName("points")
@Expose
private String points;
@SerializedName("category")
@Expose
private List<Category> category = null;
@SerializedName("quantity")
@Expose
private Integer quantity;
@SerializedName("reviews")
@Expose
private Reviews reviews;
@SerializedName("recurrings")
@Expose
private List<Object> recurrings = null;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getProductId() {
return productId;
}
private String isAddedWish = "0";
public String getIsAddedWish() {
return isAddedWish;
}
public void setIsAddedWish(String isAddedWish) {
this.isAddedWish = isAddedWish;
}
public void setProductId(Integer productId) {
this.productId = productId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Object getManufacturer() {
return manufacturer;
}
public void setManufacturer(Object manufacturer) {
this.manufacturer = manufacturer;
}
public String getSku() {
return sku;
}
public void setSku(String sku) {
this.sku = sku;
}
public String getModel() {
return model;
}
public void setModel(String model) {
this.model = model;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public List<Object> getImages() {
return images;
}
public void setImages(List<Object> images) {
this.images = images;
}
public String getOriginalImage() {
return originalImage;
}
public void setOriginalImage(String originalImage) {
this.originalImage = originalImage;
}
public List<Object> getOriginalImages() {
return originalImages;
}
public void setOriginalImages(List<Object> originalImages) {
this.originalImages = originalImages;
}
public Float getPriceExcludingTax() {
return priceExcludingTax;
}
public void setPriceExcludingTax(Float priceExcludingTax) {
this.priceExcludingTax = priceExcludingTax;
}
public Float getPrice() {
return price;
}
public void setPrice(Float price) {
this.price = price;
}
public String getPriceFormated() {
return priceFormated;
}
public void setPriceFormated(String priceFormated) {
this.priceFormated = priceFormated;
}
public Integer getRating() {
return rating;
}
public void setRating(Integer rating) {
this.rating = rating;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public List<Object> getAttributeGroups() {
return attributeGroups;
}
public void setAttributeGroups(List<Object> attributeGroups) {
this.attributeGroups = attributeGroups;
}
public Float getSpecial() {
return special;
}
public void setSpecial(Float special) {
this.special = special;
}
public Float getSpecialExcludingTax() {
return specialExcludingTax;
}
public void setSpecialExcludingTax(Float specialExcludingTax) {
this.specialExcludingTax = specialExcludingTax;
}
public String getSpecialFormated() {
return specialFormated;
}
public void setSpecialFormated(String specialFormated) {
this.specialFormated = specialFormated;
}
public String getSpecialStartDate() {
return specialStartDate;
}
public void setSpecialStartDate(String specialStartDate) {
this.specialStartDate = specialStartDate;
}
public String getSpecialEndDate() {
return specialEndDate;
}
public void setSpecialEndDate(String specialEndDate) {
this.specialEndDate = specialEndDate;
}
public List<Discount> getDiscounts() {
return discounts;
}
public void setDiscounts(List<Discount> discounts) {
this.discounts = discounts;
}
public List<Option> getOptions() {
return options;
}
public void setOptions(List<Option> options) {
this.options = options;
}
public String getMinimum() {
return minimum;
}
public void setMinimum(String minimum) {
this.minimum = minimum;
}
public String getMetaTitle() {
return metaTitle;
}
public void setMetaTitle(String metaTitle) {
this.metaTitle = metaTitle;
}
public String getMetaDescription() {
return metaDescription;
}
public void setMetaDescription(String metaDescription) {
this.metaDescription = metaDescription;
}
public String getMetaKeyword() {
return metaKeyword;
}
public void setMetaKeyword(String metaKeyword) {
this.metaKeyword = metaKeyword;
}
public String getSeoUrl() {
return seoUrl;
}
public void setSeoUrl(String seoUrl) {
this.seoUrl = seoUrl;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public String getUpc() {
return upc;
}
public void setUpc(String upc) {
this.upc = upc;
}
public String getEan() {
return ean;
}
public void setEan(String ean) {
this.ean = ean;
}
public String getJan() {
return jan;
}
public void setJan(String jan) {
this.jan = jan;
}
public String getIsbn() {
return isbn;
}
public void setIsbn(String isbn) {
this.isbn = isbn;
}
public String getMpn() {
return mpn;
}
public void setMpn(String mpn) {
this.mpn = mpn;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getStockStatus() {
return stockStatus;
}
public void setStockStatus(String stockStatus) {
this.stockStatus = stockStatus;
}
public Integer getStockStatusId() {
return stockStatusId;
}
public void setStockStatusId(Integer stockStatusId) {
this.stockStatusId = stockStatusId;
}
public Integer getManufacturerId() {
return manufacturerId;
}
public void setManufacturerId(Integer manufacturerId) {
this.manufacturerId = manufacturerId;
}
public Integer getTaxClassId() {
return taxClassId;
}
public void setTaxClassId(Integer taxClassId) {
this.taxClassId = taxClassId;
}
public String getDateAvailable() {
return dateAvailable;
}
public void setDateAvailable(String dateAvailable) {
this.dateAvailable = dateAvailable;
}
public String getWeight() {
return weight;
}
public void setWeight(String weight) {
this.weight = weight;
}
public Integer getWeightClassId() {
return weightClassId;
}
public void setWeightClassId(Integer weightClassId) {
this.weightClassId = weightClassId;
}
public String getLength() {
return length;
}
public void setLength(String length) {
this.length = length;
}
public String getWidth() {
return width;
}
public void setWidth(String width) {
this.width = width;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public Integer getLengthClassId() {
return lengthClassId;
}
public void setLengthClassId(Integer lengthClassId) {
this.lengthClassId = lengthClassId;
}
public String getSubtract() {
return subtract;
}
public void setSubtract(String subtract) {
this.subtract = subtract;
}
public String getSortOrder() {
return sortOrder;
}
public void setSortOrder(String sortOrder) {
this.sortOrder = sortOrder;
}
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getDateAdded() {
return dateAdded;
}
public void setDateAdded(String dateAdded) {
this.dateAdded = dateAdded;
}
public String getDateModified() {
return dateModified;
}
public void setDateModified(String dateModified) {
this.dateModified = dateModified;
}
public String getViewed() {
return viewed;
}
public void setViewed(String viewed) {
this.viewed = viewed;
}
public String getWeightClass() {
return weightClass;
}
public void setWeightClass(String weightClass) {
this.weightClass = weightClass;
}
public String getLengthClass() {
return lengthClass;
}
public void setLengthClass(String lengthClass) {
this.lengthClass = lengthClass;
}
public String getShipping() {
return shipping;
}
public void setShipping(String shipping) {
this.shipping = shipping;
}
public Object getReward() {
return reward;
}
public void setReward(Object reward) {
this.reward = reward;
}
public String getPoints() {
return points;
}
public void setPoints(String points) {
this.points = points;
}
public List<Category> getCategory() {
return category;
}
public void setCategory(List<Category> category) {
this.category = category;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
public Reviews getReviews() {
return reviews;
}
public void setReviews(Reviews reviews) {
this.reviews = reviews;
}
public List<Object> getRecurrings() {
return recurrings;
}
public void setRecurrings(List<Object> recurrings) {
this.recurrings = recurrings;
}
@Override
public String toString() {
return "ProductListDataModel{" +
"id=" + id +
", productId=" + productId +
", name='" + name + '\'' +
", manufacturer=" + manufacturer +
", sku='" + sku + '\'' +
", model='" + model + '\'' +
", image='" + image + '\'' +
", images=" + images +
", originalImage='" + originalImage + '\'' +
", originalImages=" + originalImages +
", priceExcludingTax=" + priceExcludingTax +
", price=" + price +
", priceFormated='" + priceFormated + '\'' +
", rating=" + rating +
", description='" + description + '\'' +
", attributeGroups=" + attributeGroups +
", special=" + special +
", specialExcludingTax=" + specialExcludingTax +
", specialFormated='" + specialFormated + '\'' +
", specialStartDate='" + specialStartDate + '\'' +
", specialEndDate='" + specialEndDate + '\'' +
", discounts=" + discounts +
", options=" + options +
", minimum='" + minimum + '\'' +
", metaTitle='" + metaTitle + '\'' +
", metaDescription='" + metaDescription + '\'' +
", metaKeyword='" + metaKeyword + '\'' +
", seoUrl='" + seoUrl + '\'' +
", tag='" + tag + '\'' +
", upc='" + upc + '\'' +
", ean='" + ean + '\'' +
", jan='" + jan + '\'' +
", isbn='" + isbn + '\'' +
", mpn='" + mpn + '\'' +
", location='" + location + '\'' +
", stockStatus='" + stockStatus + '\'' +
", stockStatusId=" + stockStatusId +
", manufacturerId=" + manufacturerId +
", taxClassId=" + taxClassId +
", dateAvailable='" + dateAvailable + '\'' +
", weight='" + weight + '\'' +
", weightClassId=" + weightClassId +
", length='" + length + '\'' +
", width='" + width + '\'' +
", height='" + height + '\'' +
", lengthClassId=" + lengthClassId +
", subtract='" + subtract + '\'' +
", sortOrder='" + sortOrder + '\'' +
", status='" + status + '\'' +
", dateAdded='" + dateAdded + '\'' +
", dateModified='" + dateModified + '\'' +
", viewed='" + viewed + '\'' +
", weightClass='" + weightClass + '\'' +
", lengthClass='" + lengthClass + '\'' +
", shipping='" + shipping + '\'' +
", reward=" + reward +
", points='" + points + '\'' +
", category=" + category +
", quantity=" + quantity +
", reviews=" + reviews +
", recurrings=" + recurrings +
'}';
}
}
我尝试通过以下方法通过意图发送对象
private ArrayList<ProductListDataModel> productList;
Bundle bundle = new Bundle();
bundle.putSerializable("data",productList );
intent.putExtras(bundle);
但这会导致应用崩溃。我崩溃的错误是
"UncaughtException:
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:751)
at android.app.Activity.startActivity(Activity.java:4800)"
我已经通过在线使用jsonschema2pojo创建了模型类。 任何人都可以给我建议解决方案。
答案 0 :(得分:1)
进行这些更改
在模型类中实现Serializable
。
public class ProductListDataModel implements Serializable{
//your rest code
}
答案 1 :(得分:0)
您Serializable
中的实现Parcelable
或ProductListDataModel
例如:
public class ProductListDataModel implements Serializable{
// your code
}
或使用Parcelable:
import android.os.Parcel;
import android.os.Parcelable;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
public class ProductListDataModel implements Parcelable {
@SerializedName("id")
@Expose
private Integer id;
protected ProductListDataModel(Parcel in) {
if (in.readByte() == 0) {
id = null;
} else {
id = in.readInt();
}
}
public static final Creator<ProductListDataModel> CREATOR = new Creator<ProductListDataModel>() {
@Override
public ProductListDataModel createFromParcel(Parcel in) {
return new ProductListDataModel(in);
}
@Override
public ProductListDataModel[] newArray(int size) {
return new ProductListDataModel[size];
}
};
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
@Override
public String toString() {
return "ProductListDataModel{" +
"id=" + id +
'}';
}
@Override
public int describeContents() {
return hashCode();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
if (id == null) {
dest.writeByte((byte) 0);
} else {
dest.writeByte((byte) 1);
dest.writeInt(id);
}
}
}
答案 2 :(得分:0)
必须使用parcelable
实现,一旦实现,就必须在模型类中添加实现的方法,该方法由android studio自动生成。
然后以这种方式故意发送对象
Intent intent = new Intent(this,CallFragmentOrActivity.class);
intent.putParcelableArrayListExtra("data", productList);
getActivity().setResult(Activity.RESULT_OK, intent);
getActivity().finish();
然后在被调用的活动或片段中获取parcelable
对象
Intent intent = getActivity().getIntent();
if (intent != null) {
yourProductListObject = intent.getParcelableArrayListExtra("data");
}
我希望这会对您有所帮助:)