classcastexception - Error with unpacking a Custom Data Type from a Parcel in Android -


i have problem of recreating array of custom data type after written parcel.

playerwear declared such:

private wearable playerwear[] = new wearable[5]; 

this lines write parcel in parent class. array of type wearable 5 elements

        parcel.writearray(playerwear); 

this line reads parcel (temp empty data element of parent , has elements filled in same fashion bellow. others working)

        temp.playerwear = (wearable[])source.readarray(wearable.class.getclassloader()); 

here code of parcel interface defined in wearable datatype:

    @override public int describecontents() {     return 0; }  /**  * main method of parcelable interface.  packs  * parcel used in next activity  * @param parcel parcel passed next activity  * @param used keep track of 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"); }  /**  * method takes passed parcel , rebuilds can used  * @param source passed parcel  * @return 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 sure think similar classloader  */ public static final parcelable.creator creator = new parcelable.creator() {     /**      * calls method created recreate data type      * @param in passed parcel      * @return s return of recreateparcel method      */     public wearable createfromparcel(parcel in)     {         return recreateparcel(in);     }      /**      * called if custom data type in array      * @param size size of array. used figure out how many elements needed      * @return array of data type      */     public wearable[] newarray(int size)     {         return new wearable[size];     } }; 

lastly error giving me,

caused by: java.lang.classcastexception: java.lang.object[] cannot cast com.rtbd.storytime.wearable[]

please post if forgot vital information needed solve problem.

thanks!

after doing lot more research found answer at: read & writing arrays of parcelable objects

you don't want write parcelablearray or array want write typedarray , call creator of data type in createtypedarray method.


Comments