Can't pass an ArrayList<Parcelable> to an activity

半城伤御伤魂 提交于 2019-12-01 17:49:52

问题


This is the code

ArrayList<MyObject> list = new ArrayList<MyObject>();
list.add(new MyObject());
Intent intent = new Intent(this, ReceiverActivity.class);
intent.putExtra("list", list);
startActivity(intent);

ReceiverActivity

List<MyObject> list = (List<MyObject>)getIntent().getExtras().getParcelable("list");

Here list is null. Also this doesn't work:

List<MyObject> list = (List<MyObject>)getIntent().getExtras().getSerializable("list");

MyObject is Parcelable, I implemented all required methods. I guess this implementation is not the problem, because otherwise I would recive other kind of exceptions. But I don't get anything besides list is null.

Thanks in advance...

Now I found this:

List<Parcelable> list = (List<Parcelable>)getIntent().getParcelableArrayListExtra("list");

that has to be used in the receiver activity, but how do I send it and how do I get List<MyObject> from List<Parcelable> ?


回答1:


USe i.putParcelableArrayListExtra(name, value) where i is your intent. Dont use putExtra() for a parcelable ArrayList.




回答2:


Try:

ArrayList<MyObject> myList = extras.<MyObject>getParcelableArrayList("list"));


来源:https://stackoverflow.com/questions/11190191/cant-pass-an-arraylistparcelable-to-an-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!