0

I want to pass MyObj[ ] between activities.

I'm using an Intent and Parcelable to do so, but when I try to receive that data in Activity B I get the error: "java.lang.ClassCastException: android.os.Parcelable[] cannot be cast to com.hfad.myapp.MyObj[ ]".

I saw that many people had problems with this but couldn't find a solution to my case.

This answer doesn't solve it android.os.Parcelable[] cannot be cast to... Error

MyObj class

@Entity (tableName = "myobj")
public class MyObj implements Parcelable {
    @PrimaryKey(autoGenerate = true)
    private int id;

    private String productName;
    private int numberOfproducts;

    protected MyObj (Parcel in) {
        id = in.readInt();
        productName = in.readString();
        numberOfproducts = in.readInt();
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(id);
        dest.writeString(productName);
        dest.writeInt(numberOfproducts);
    }

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

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

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

    public int getId() {
        return id;
    }

    public String getProductName() {
        return productName;
    }

    public int getNumberOfproducts() {
        return numberOfproducts;
    }

    public void setId(int id) {
        this.id = id;
    }


    public MyObj(String productName, int numberOfproducts) {
        this.productName = productName;
        this.numberOfproducts = numberOfproducts;

    }
}

Activity A

Intent intent = new Intent(getContext(), ActivityB.class);
intent.putExtra("array", array);
startActivity(intent);

Activity B

//Here the error occurs
MyObj[] myArray = (MyObj[]) getIntent().getParcelableArrayExtra("array");

Is there a way to fix this? Or should I use a different approach to pass data between Activities?

Kamil Radz
  • 47
  • 1
  • 10

0 Answers0