I have an error when using a Parcelable Class. I'm trying to pass an image as a ByteArray between activities, and eventually to a remote webservice.
java.lang.ClassNotFoundException: ... Class not found when unmarshalling:
import java.util.UUID;
public class Property implements Parcelable {
@SerializedName("id")
@NonNull
private final String mId;
protected byte[] mImage;
public byte[] getmImage() { return mImage; }
public void setmImage(byte[] image){ mImage = image; }
@SerializedName("generalInformation")
private GeneralInformation mGeneralInformation;
@NonNull
public GeneralInformation getGeneralInformation() {
return mGeneralInformation;
}
public void setGeneralInformation(@NonNull final GeneralInformation generalInformation) {
mGeneralInformation = generalInformation;
}
@NonNull
public String getId() {
return mId;
}
@Override
public boolean equals(final Object o) {
return (o instanceof Property) && ((Property) o).mId.equals(mId);
}
@Override
public int hashCode() {
return mId.hashCode();
}
public static Property create() {
final Property model = new Property(UUID.randomUUID().toString());
model.setGeneralInformation(new GeneralInformation());
return model;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(@NonNull final Parcel dest, final int flags) {
if(mImage != null) {
dest.writeInt(mImage.length);
dest.writeByteArray(mImage);
}
dest.writeString(mId);
dest.writeParcelable(mGeneralInformation, 0);
}
private Property(@NonNull final String id) {
mId = id;
}
protected Property(final Parcel in) {
if(mImage != null)
{
mImage = new byte[in.readInt()];
in.createByteArray();
}
mId = in.readString();
mGeneralInformation = in.readParcelable(GeneralInformation.class.getClassLoader());
}
public static final Parcelable.Creator<Property> CREATOR = new Parcelable.Creator<Property>() {
public Property createFromParcel(@NonNull final Parcel source) {
return new Property(source);
}
@NonNull
public Property[] newArray(final int size) {
return new Property[size];
}
};
}
Now I have a very limited understanding of the problem(s) I have with this class. mImage is null when reading the Parcel. (Even when I know for sure that the byteArray is not null when I assign it to the Parcel.) I have tried to initialize mImage in its declaration so it not null but then I have that Class not found when unmarshalling error...
I'm almost sure the problem lies in
protected Property(final Parcel in) {
if(mImage != null)
{
mImage = new byte[in.readInt()];
in.createByteArray();
}
But I don't know how to fix it.
I'm lost for 2 days with this thing :/
Please see the below code for constructor with Parcel object.
protected Property(final Parcel in) {
mImage = new byte[in.readInt()];
in.readByteArray(mImage); // this will read the byte array from Parcel object(in) and store the value in mImage member variable.
mId = in.readString();
mGeneralInformation = in.readParcelable(GeneralInformation.class.getClassLoader());
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.