我正在尝试从另一个活动中获取字符串的ArrayList,并打印出这样的元素:
ArrayList<String> foo;
if (resultCode == RESULT_OK) { //this does get called
Intent extras = this.getIntent();
foo = extras.getStringArrayListExtra("foo");
for (int i = 0; i < foo.size(); i++) //error, null object
System.out.println(foo.get(i));
}
在getStringArrayListExtra之前实例化它对错误无效:
java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
我怎样才能获得ArrayList的大小?
这就是我传递它的方式:
Intent intent = new Intent();
intent.putStringArrayListExtra("foo",foo);
setResult(RESULT_OK, intent);
我确保在传递
之前有数据答案 0 :(得分:0)
onActivityResult (int requestCode, int resultCode, Intent data)
使用收到的onActivityResult
意图,不 this.getIntent()