2015-06-03 2 views
0

У меня возникла проблема воссоздания массива пользовательского типа данных после его написания на посылке.Ошибка при распаковке пользовательского типа данных с посылки в Android

playerwear объявлен как таковой:

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

Это линии, которые записывают его посылки в родительском классе. Это массив типа носимого с 5 элементами

 parcel.writeArray(playerWear); 

Это линия, которая считывает его из посылок (температура является пустым элементом данных родителя и имеет все элементы, наполненные таким же образом, как показано ниже. Все остальные работают)

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

Вот код Parcel интерфейса, которые определены в носимого типа данных:

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

Пожалуйста если я забыл какую-либо важную информацию, необходимую для решения этой проблемы.

Спасибо!

ответ

0

После этого намного больше исследований, я нашел ответ на: Read & writing arrays of Parcelable objects

Вы не хотите писать как parcelableArray или как массив, который вы хотите записать его как TypedArray, а затем вызвать создатель тип данных в методе createTypedArray.

Смежные вопросы