2015-06-03 29 views
0

我有這樣的問題,它在寫入包裹後重新創建自定義數據類型的數組。在Android中從包中打開自定義數據類型的錯誤

playerwear被聲明爲這樣:

private Wearable playerWear[] = new Wearable[5]; 

這是把它寫入在父類包裹的行。 它是類型可穿戴的陣列具有5個元素

 parcel.writeArray(playerWear); 

這是從包裹讀取它(溫度是父的一個空的數據元素,並具有填充在相同的方式波紋管的所有元素的行。所有其他人共同)

 temp.playerWear = (Wearable[])source.readArray(Wearable.class.getClassLoader()); 

這是在可穿戴數據類型中定義的的包裹界面的代碼:

@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 []

請如果我忘記了解決此問題所需的任何重要信息,請發帖。

謝謝!

回答