[英]Android Listview: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
I am trying to create a list based on the listview elements and when I go beyond a certain amount of elements in a list and try to save, I get this error.我正在尝试基于 listview 元素创建一个列表,当我 go 超出列表中的一定数量的元素并尝试保存时,我收到此错误。 Here is my code:
这是我的代码:
private void SaveList() {
/** get all values of the EditText-Fields */
View v;
EditText et;
TextView tv;
for (int i = 0; i < lv.getCount(); i++) {
v = lv.getChildAt(i);
et = (EditText) v.findViewById(R.id.editid);
tv = v.findViewById(R.id.field);
EditModel saveDataItem = new EditModel(); //edit model is just an object containing values of the listview elements
saveDataItem.setEditTextValue(et.getText().toString());
saveDataItem.setFieldName(tv.getText().toString());
saveList.add(saveDataItem);
}
}
And I declare savelist when the activity is created like this:我在创建活动时声明保存列表,如下所示:
private ArrayList<EditModel> saveList = new ArrayList<>();
I do not get an error when the listview is less than the length 8当列表视图小于长度 8 时,我没有收到错误消息
Oh, I found a solution, I had to make sure that I use listview.getChildCount() instead of listview.getCount()哦,我找到了解决方案,我必须确保我使用 listview.getChildCount() 而不是 listview.getCount()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.