在第一个 Activity 中,我有一个必须传递给第二个 Activity 的 ArrayList。
这是第一个 Activity :
public ArrayList<ItemContact> selectedContacts = new ArrayList<>(); //filled in the rest of the code
Intent intent = new Intent(this, SummaryActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("selectedContacts", selectedContacts);
intent.putExtra("selectedContacts", bundle);
startActivity(intent);
在第二个 Activity 中:
ArrayList<ItemContact> selectedContacts = new ArrayList<>();
selectedContacts = (ArrayList<ItemContact>)getIntent().getExtras().getSerializable("selectedContacts") ;
问题是第二个 Activity 中的 selectedContacts 始终为 null 我该如何修复它?
编辑:ItemContact 已经实现了 Serialized,但仍然不起作用
最佳答案
你的对象应该实现Serialized
class ItemContact implements Serializable {
......
}
第一个 Activity
public ArrayList<ItemContact> selectedContacts = new ArrayList<>();
Intent intent = new Intent(this, SummaryActivity.class);
Bundle bundle = new Bundle();
bundle.putSerializable("selectedContacts", selectedContacts);
intent.putExtras(bundle);
startActivity(intent);
第二个 Activity
ArrayList<ItemContact> selectedContacts = new ArrayList<>();
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
selectedContacts = (ArrayList<ItemContact>)bundle.getSerializable("selectedContacts");
希望这有帮助。
关于android - ArrayList<Object> 使用 getSerialized 问题到第二个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58868633/