I am working in Android Studio and any time I need Parcelable
just auto generate the code. My scenario is that i have super class A
and class B
extended from class A
. I'd like to pass through an intent class B
but when I intercept it I get an error sayng Caused by: java.lang.ClassCastException: pack.A cannot be cast to pack.B
. The code is:
public class A implements Parcelable {
private String str1;
protected A(Parcel in) {
str1 = in.readString();
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(str1);
}
@Override
public int describeContents() {
return 0;
}
public static final Creator<A> CREATOR = new Creator<A>() {
@Override
public A createFromParcel(Parcel in) {
return new A(in);
}
@Override
public A[] newArray(int size) {
return new A[size];
}
};
}
public class B extends A {
private String str2;
protected B(Parcel in) {
super(in);
}
}
I'm confused about this error. Any help is apreciated