-1

I have a RecyclerView and onclicking any item I want to start an activity by sending the object corresponding to the item via intent. But app crashes when i try to send the object. My Model class

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

}

I tried to send the object via intent as the following method

private ArrayList<ProductListDataModel> productList;

Bundle bundle = new Bundle();
                bundle.putSerializable("data",productList );
                intent.putExtras(bundle);

But this causes the app to crash. The error I get on crashing is

"UncaughtException:     
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:751)
            at android.app.Activity.startActivity(Activity.java:4800)"

I have created the model class by using jsonschema2pojo online. Any one please suggest me a solution.

visakh r
  • 175
  • 1
  • 11
  • 2
    Implement _ProductListDataModel_ with _Serializable_ or _Parcable_ – Piyush Dec 21 '18 at 06:47
  • [How can I make my custom objects Parcelable?](https://stackoverflow.com/questions/7181526/how-can-i-make-my-custom-objects-parcelable) – RonTLV Dec 21 '18 at 06:53
  • Also _productList_ should not be empty – Piyush Dec 21 '18 at 06:55
  • not working , again crashes with error "UncaughtException: java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.myapp.test.model.model_product_list.ProductListDataModel) at android.os.Parcel.writeSerializable" – visakh r Dec 21 '18 at 06:56

3 Answers3

1

do these changes

implement Serializable in your model class.

 public class ProductListDataModel  implements Serializable{

    //your rest code 

    }
Sachin Rajput
  • 4,326
  • 2
  • 18
  • 29
  • not working,getting error as "UncaughtException: java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.myapp.test.model.model_product_list.ProductListDataModel) at android.os.Parcel.writeSerializable" – visakh r Dec 21 '18 at 06:53
1

You must implement with parcelable, once you implemented it, you have to add implemented methods in your model class, which automatically generates by android studio.

And then send your object in intent like this way

    Intent intent = new Intent(this,CallFragmentOrActivity.class);
    intent.putParcelableArrayListExtra("data", productList);
    getActivity().setResult(Activity.RESULT_OK, intent);
    getActivity().finish();

And then get your parcelable object in your called activity or fragment

    Intent intent = getActivity().getIntent();
    if (intent != null) {
        yourProductListObject = intent.getParcelableArrayListExtra("data");
    }

I hope this will help you :)

Ritesh Adulkar
  • 841
  • 11
  • 18
0

implement Serializable or Parcelable in you ProductListDataModel

for eg:

public class ProductListDataModel implements Serializable{ 


// your code

}

or By using 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);
        }

    }
}
Dhruv Tyagi
  • 814
  • 1
  • 9
  • 28
  • not working. app crases again. "UncaughtException: java.lang.RuntimeException: Parcelable encountered IOException writing serializable object (name = com.myapp.test.model.model_product_list.ProductListDataModel) at android.os.Parcel.writeSerializable" – visakh r Dec 21 '18 at 06:54
  • when implementing parcelable, recycleradapter binding not working. – visakh r Dec 21 '18 at 07:32