Flo*_*Flo 5 android parcelable android-intent android-activity
我正在尝试通过Intent对象将Address对象数组传递给另一个Activity.
当Address类实现Parcelable接口时,我尝试执行以下操作.我从Geocoder对象获得了一个List Address对象,我将其转换为Address对象数组.然后我将此数组放入Intent并调用该活动.
final Address[] addresses = addresseList.toArray(new Address[addresseList.size()]);
final Intent intent = new Intent(this, SelectAddress.class);
intent.putExtra(SelectAddress.INTENT_EXTRA_ADDRESSES, startAddresses);
startActivityForResult(intent, REQUEST_CODE_ACTIVITY_SELECT_ADDRESSES);
Run Code Online (Sandbox Code Playgroud)
在其他活动中,我尝试使用以下代码从Intent中检索Address [].但是最后一行的调用以ClassCastException结束Landroid.os.Parcelable.
Bundle bundle = getIntent().getExtras();
Address[] addresses = (Address[]) bundle.getParcelableArray(INTENT_EXTRA_ADDRESSES);
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?我如何检索地址[].
Lio*_*orZ 14
问题是铸造.尝试:
Bundle bundle = getIntent().getExtras();
Parcelable[] parcels = bundle.getParcelableArray(INTENT_EXTRA_ADDRESSES);
Address[] addresses = new Address[parcels.length];
for (Parcelable par : parcels){
addresses.add((Address) par);
}
Run Code Online (Sandbox Code Playgroud)
小智 5
or on java1.6:
Parcelable[] x = bundle.getParcelableArray(KEY);
addresses = Arrays.copyOf(x, x.length, Address[].class);
| 归档时间: |
|
| 查看次数: |
5182 次 |
| 最近记录: |