简体   繁体   English

Android Listview: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference

[英]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.

相关问题 FacebookSdk尝试在空对象引用上调用虚拟方法&#39;android.view.View android.view.View.findViewById(int)&#39; - FacebookSdk Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference 尝试在空对象引用上调用虚拟方法&#39;android.view.View android.view.View.findViewById(int)&#39; - Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference java.lang.NullPointerException:尝试在空对象引用上调用虚方法&#39;android.view.View android.view.View.findViewById(int)&#39; - java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference Attempt to invoke virtual method 'android.view.View android.widget.ImageView.findViewById(int)' on a null object reference - Attempt to invoke virtual method 'android.view.View android.widget.ImageView.findViewById(int)' on a null object reference 尝试在空对象引用上调用虚拟方法“android.view.View com.google.android.exoplayer2.ui.PlayerView.findViewById(int)” - Attempt to invoke virtual method 'android.view.View com.google.android.exoplayer2.ui.PlayerView.findViewById(int)' on a null object reference 从ListView获取子对象会导致在null对象引用上出现android.view.View android.view.View.findViewById(int) - Getting child from ListView causes android.view.View android.view.View.findViewById(int) on a null object reference 如何修复在 null object 参考上调用虚拟方法 'android.view.View androidx.fragment.app.FragmentActivity.findViewById(int)' 的尝试? - How to fix attempt to invoke virtual method 'android.view.View androidx.fragment.app.FragmentActivity.findViewById(int)' on a null object reference? java.lang.NullPointerException:尝试在空对象引用上调用虚方法'android.view.View .MainActivity.findViewById(int)' - java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View .MainActivity.findViewById(int)' on a null object reference java.lang.NullPointerException:尝试在 null object 参考上调用虚拟方法“android.view.View android.app.Activity.findViewById(int)” - java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.app.Activity.findViewById(int)' on a null object reference 我收到此错误:尝试在 null ZA8CFDE6331BD59EB2AC96F8911C4B66Z 参考上调用虚拟方法 'android.view.View android.widget.Button.findViewById(int)' - I'm getting this error: Attempt to invoke virtual method 'android.view.View android.widget.Button.findViewById(int)' on a null object reference
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM