在Android中从包裹解包自定义数据类型时出错

在Android中从包裹解包自定义数据类型时出错,android,classcastexception,parcelable,parcel,Android,Classcastexception,Parcelable,Parcel,在将自定义数据类型的数组写入地块后,我遇到了重新创建该数组的问题 playerwear声明如下: private Wearable playerWear[] = new Wearable[5]; 这是将其写入父类中地块的线。 这是一个5元素可穿戴的阵型 parcel.writeArray(playerWear); 这是从地块读取数据的行(temp是父元素的空数据元素,所有元素的填充方式与下面相同。所有其他元素都在工作) 以下是可穿戴数据类型中定义的包裹接口代码: @O

在将自定义数据类型的数组写入地块后,我遇到了重新创建该数组的问题

playerwear声明如下:

private Wearable playerWear[] = new Wearable[5];
这是将其写入父类中地块的线。 这是一个5元素可穿戴的阵型

        parcel.writeArray(playerWear);
这是从地块读取数据的行(temp是父元素的空数据元素,所有元素的填充方式与下面相同。所有其他元素都在工作)

以下是可穿戴数据类型中定义的包裹接口代码:

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

/**
 * This is the main method of the Parcelable Interface.  This packs everything
 * up into the parcel that will be used in the next activity
 * @param parcel the parcel that will be passed to the next activity
 * @param i used to keep track of the size...I think
 */
@Override
public void writeToParcel(Parcel parcel, int i)
{
    parcel.writeString(slotName);
    parcel.writeParcelable(heldItem, i);
    if (holdingItem)
        parcel.writeString("True");
    else
        parcel.writeString("False");
}

/**
 * This is the method that takes the passed parcel and rebuilds it so that it can be used
 * @param source the passed parcel
 * @return the reconstructed data type
 */
public static Wearable recreateParcel(Parcel source)
{
    Wearable temp = new Wearable();
    temp.slotName = source.readString();
    temp.heldItem = (Item) source.readParcelable(Item.class.getClassLoader());
    String bool = source.readString();
    if(bool.equals("True"))
        temp.holdingItem = true;
    else
        temp.holdingItem = false;

    return temp;
}

/**
 * Not truly sure what this is but I think this is similar to the classLoader
 */
public static final Parcelable.Creator CREATOR = new Parcelable.Creator()
{
    /**
     * Calls the method that I created to recreate the Data type
     * @param in the passed parcel
     * @return s the return of the recreateParcel method
     */
    public Wearable createFromParcel(Parcel in)
    {
        return recreateParcel(in);
    }

    /**
     * Called if the custom data type is in an array
     * @param size the size of the array. Used to figure out how many elements are needed
     * @return an Array of the Data type
     */
    public Wearable[] newArray(int size)
    {
        return new Wearable[size];
    }
};
最后这是它给我的错误

原因:java.lang.ClassCastException:java.lang.Object[]无法强制转换为com.rtbd.storytime.Wearable[]

如果我忘记了解决此问题所需的任何重要信息,请发布


谢谢

在做了大量研究之后,我在以下位置找到了答案:

您不希望以parcelableArray或数组的形式写入,而是希望以TypedArray的形式写入,然后在createTypedArray方法中调用数据类型的创建者

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

/**
 * This is the main method of the Parcelable Interface.  This packs everything
 * up into the parcel that will be used in the next activity
 * @param parcel the parcel that will be passed to the next activity
 * @param i used to keep track of the size...I think
 */
@Override
public void writeToParcel(Parcel parcel, int i)
{
    parcel.writeString(slotName);
    parcel.writeParcelable(heldItem, i);
    if (holdingItem)
        parcel.writeString("True");
    else
        parcel.writeString("False");
}

/**
 * This is the method that takes the passed parcel and rebuilds it so that it can be used
 * @param source the passed parcel
 * @return the reconstructed data type
 */
public static Wearable recreateParcel(Parcel source)
{
    Wearable temp = new Wearable();
    temp.slotName = source.readString();
    temp.heldItem = (Item) source.readParcelable(Item.class.getClassLoader());
    String bool = source.readString();
    if(bool.equals("True"))
        temp.holdingItem = true;
    else
        temp.holdingItem = false;

    return temp;
}

/**
 * Not truly sure what this is but I think this is similar to the classLoader
 */
public static final Parcelable.Creator CREATOR = new Parcelable.Creator()
{
    /**
     * Calls the method that I created to recreate the Data type
     * @param in the passed parcel
     * @return s the return of the recreateParcel method
     */
    public Wearable createFromParcel(Parcel in)
    {
        return recreateParcel(in);
    }

    /**
     * Called if the custom data type is in an array
     * @param size the size of the array. Used to figure out how many elements are needed
     * @return an Array of the Data type
     */
    public Wearable[] newArray(int size)
    {
        return new Wearable[size];
    }
};