通过Intent返回字符串的ArrayList时,我获取null值

时间:2017-12-10 14:29:51

标签: java android android-intent arraylist null

我正在制作一个邮寄应用。我想要做的是从 composeMailActivity 打开列表 chooseContactsActivity ,选择一些联系人,然后点击添加将其发回到composeMailActivity。我做的第一件事是标准在我传递string的活动之间传递,并且在composeMailActivity中传递并识别了正确的字符串。然后我添加了arrayList strings个联系ID。在chooseContactsActivity中,在调试时可以正确识别它,但在composeMailActivity中它会返回null

我尝试过的一些答案包括:12345 ......

ChooseContactsActivity:

addButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            ArrayList<String> selectedIds = adapter.selectedIds;

            String text = "abc";

            Intent intent = new Intent();
            intent.putStringArrayListExtra("contacts_added", selectedIds);
            setResult(RESULT_OK, intent);
            finish();
        }
});

ComposeMailActivity:

 @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
            ArrayList<String> get_contacts = getIntent().getExtras().getStringArrayList("contacts_added");
        }
    }
}

2 个答案:

答案 0 :(得分:4)

我想尝试获取&#34; contacts_added&#34;表格&#34;数据&#34;。而不是调用getIntent()尝试

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == 1) {
        if (resultCode == RESULT_OK) {
            ArrayList<String> get_contacts = data.getExtras().getStringArrayList("contacts_added");
        }
    }
}

希望这能解决您的问题。

答案 1 :(得分:4)

您应该使用data中的Intent onActivityResult(int requestCode, int resultCode, Intent data)代替getIntent() 像这样

if (resultCode == RESULT_OK) {
            ArrayList<String> get_contacts = data.getExtras().getStringArrayList("contacts_added");
        }