如何将 Address 对象数组传递给其他 Acitvity
我试图通过 Address 对象数组传递给另一个 Activity一个意图对象。
由于 Address 类实现 Parcelable 接口,我尝试执行以下操作。我从地理编码器对象中获取了列表地址对象,并将其转换为地址对象数组。然后我将这个数组放入 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);
在另一个活动中,我尝试使用以下代码从 Intent 中检索 Address[]。但最后一行的调用以 ClassCastException Landroid.os.Parcelable
结束。
Bundle bundle = getIntent().getExtras();
Address[] addresses = (Address[]) bundle.getParcelableArray(INTENT_EXTRA_ADDRESSES);
我做错了什么?我如何检索地址[]。
I'm trying to pass an array of Address objects to another Activity through an Intent object.
As the Address class implements the Parcelable interface I try to do the following. I got a List Address object from a Geocoder object, which I convert into a array of Address objects. Then I put this array into the Intent and call the activity.
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);
On the other activity I try to retrieve the Address[] from the Intent with the following piece of code. But the call of the last line ends with a ClassCastException Landroid.os.Parcelable
.
Bundle bundle = getIntent().getExtras();
Address[] addresses = (Address[]) bundle.getParcelableArray(INTENT_EXTRA_ADDRESSES);
What am I doing wrong? How do I have to retrieve the Address[].
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
问题在于铸造。尝试:
The problem is the casting. try:
@LiorZ 答案 完全正确。我刚刚将他的答案和这个其他合并到这个方便的函数中。
The @LiorZ answer is completely true. I just merged his answer and this other in this handy function.