首页 > 代码库 > Android -- android.os.Parcelable[] cannot be cast to ...

Android -- android.os.Parcelable[] cannot be cast to ...

我本想直接把Bunde.getParcelableArray(...)得到的Parcelable[]强制转换为自定义类数组,但是失败了,网上找了两种解决办法:

Parcelable[] data =http://www.mamicode.com/bundleGet.getParcelableArray(MessageApi.ARG_GET_DATA_RESULT);
CustomData[] customData = http://www.mamicode.com/new CustomData[data.length];
// This approach is ok.
// customData = http://www.mamicode.com/Arrays.copyOf(data, data.length, CustomData[].class);
// This approach is ok too.
for(int i = 0; i < data.length; i++){
customData[i] = (CustomData) data[i];
}

一种是通过Arrays.copeOf(...).

一种是通过for循环,一个一个的强制转换。

Android -- android.os.Parcelable[] cannot be cast to ...