通过意图传递对象时应用程序崩溃
Posted
技术标签:
【中文标题】通过意图传递对象时应用程序崩溃【英文标题】:app crashes when passing object via intent 【发布时间】:2019-05-21 15:53:50 【问题描述】:我有一个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 创建了模型类。 任何人都请给我一个解决方案。
【问题讨论】:
使用 Serializable 或 Parcable 实现 ProductListDataModel How can I make my custom objects Parcelable? 另外 productList 不能为空 不工作,再次崩溃并出现错误“UncaughtException:java.lang.RuntimeException:Parcelable 遇到 IOException 在 android.os 写入可序列化对象(名称 = com.myapp.test.model.model_product_list.ProductListDataModel)。 Parcel.writeSerializable" 【参考方案1】:做这些改变
在你的模型类中实现Serializable
。
public class ProductListDataModel implements Serializable
//your rest code
【讨论】:
不工作,收到错误为“UncaughtException:java.lang.RuntimeException:Parcelable 遇到 IOException 在 android.os.Parcel 写入可序列化对象(名称 = com.myapp.test.model.model_product_list.ProductListDataModel) .writeSerializable"【参考方案2】:你必须用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");
希望对你有帮助:)
【讨论】:
【参考方案3】:在你的ProductListDataModel
中实现Serializable
或Parcelable
例如:
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);
【讨论】:
不工作。应用程序再次崩溃。 “UncaughtException: java.lang.RuntimeException: Parcelable 遇到 IOException 在 android.os.Parcel.writeSerializable 写入可序列化对象 (name = com.myapp.test.model.model_product_list.ProductListDataModel)” 在实现 parcelable 时,recycleradapter 绑定不起作用。以上是关于通过意图传递对象时应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章
当我使用要传递餐厅 ID 的意图将数据从回收器适配器传递到活动时,我的应用程序崩溃了