【发布时间】:2014-02-16 10:54:06
【问题描述】:
我创建了一个数组列表和一个 ListView。我打算遍历 ListView,并检查它们是否被选中,然后将对象(使用 ListView.getItem())添加到我的数组列表中。但是我得到一个 NullPointerException。 ArrayList people_selected,在类的顶部声明如下:
ArrayList<PeopleDetails> selected_people;
还有我的代码:
for (int i = 0; i < people_list.getCount(); i++) {
item_view = people_list.getAdapter().getView(i, null, null);
chBox = (CheckBox) item_view.findViewById(R.id.checkBox);//your xml id value for checkBox.
if (chBox.isChecked()) {
selected_people.add((PeopleDetails) people_list.getItemAtPosition(i));
}
}
for(PeopleDetails person : selected_people){
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(person.number, null, sms_message, null, null);
}
///and now need to write party to file.
我在线路上遇到错误
for(PeopleDetails person : selected_people)
说“NullPointerException” .我认为这意味着数组列表为空,并且无法弄清楚为什么它应该为空。我是否在班上宣布错了?还是我的选择和添加方法有问题?
【问题讨论】:
-
添加 "ArrayList
people_selected = new ArrayList (); -
您在 declation 和添加到项目部分时为 arraylist 使用不同的变量名?
-
向一个对象声明一个引用变量只不过是为您保留空间以将其设置为一个真实对象的引用在将来的某个时间。在您将其设置为引用之前,它不会指向任何内容或 null,这会在您尝试取消引用它时导致空指针异常(纯粹主义者,我知道统一化的空引用指向空对象,但为了简单的注释....)
标签: java android listview arraylist nullpointerexception