【问题标题】:Nullpointerexception on getChildAtgetChildAt 上的空指针异常
【发布时间】:2013-07-29 09:02:22
【问题描述】:

我有一个带有一些复选框的列表视图,我需要根据存储在数组中的一些数据自动检查它们。我的扩展基本适配器的自定义适配器:

 public class MyAdapter extends BaseAdapter 
    {
        private Context context;

        public SPCMjereAdapter(Context c) 
        {           
            context = c;                    
        }

        public int getCount() {
            return MyArrList.size();
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public int getlistItemsCount()
        {
            return listView.getChildCount();
        }

        public View getView(final int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if (convertView == null) {
                convertView = inflater.inflate(R.layout.activity_list_row, null);

            }   
            // ColID
            TextView txtOpis = (TextView) convertView.findViewById(R.id.ColOpis); 
            txtOpis.setText(MyArrList.get(position).get("OpisMjere") +".");

            // ColCode
            TextView txtRbMjere = (TextView) convertView.findViewById(R.id.ColCode);
            txtRbMjere.setText(MyArrList.get(position).get("RbMjere"));


            // ColChk               
            CheckBox Chk = (CheckBox) convertView.findViewById(R.id.ColChk);
            Chk.setTag(MyArrList.get(position).get("RbMjere"));


            return convertView;

        }

    }

这就是我检查项目的方式

int k=0;
    int j=0;
    for (j=0; j<numberOfItems; j++)
    {

        LinearLayout itemLayout = (LinearLayout)listView.getChildAt(j); // Find by under LinearLayout
        CheckBox checkbox = (CheckBox)itemLayout.findViewById(R.id.ColChk);

        for (k=0; k<rbmjere.size(); k++)
        {
            if (checkbox.getTag().toString() == rbmjere.get(k).toString())
            {
                checkbox.setChecked(true);
            }
        }   
    }

问题出在一行

LinearLayout itemLayout = (LinearLayout)listView.getChildAt(j);

因此,如果我调用此代码来检查项目,问题是 listview 显示例如 3 个项目,但代码仅识别 2 个项目并且缺少第三个项目。如何检测所有项目何时可见或如何检测列表视图何时渲染完成?

【问题讨论】:

  • 我建议你在for循环中使用int count = listView.getChildCount();然后for (j=0; j&lt;count; j++)
  • 看看你的 for 循环 - 它只循环两次 - 对于 0 和 1(它是
  • 但是该放在哪里呢?我想在 listview 加载所有数据后检查项目。我不确定我的逻辑是否正确......
  • 问题是把那个循环放在哪里?在 getView() 内部还是其他地方?
  • 您无需转换为 LinearLayout 即可使用 findViewById。您无需强制转换为 Checkbox 即可使用 setTag。 numberOfItems 应该作为 listView.getChildCount 获得,它返回在 ListView 中可见的项目(不要与“所有项目都在那里以适配器.getCount 的长度创建”混淆)这是什么 - checkbox.getTag().toString() == rbmjere.get(k).toString() ?字符串不能像这样比较。如果要比较对象实例,则不必将它们转换为字符串。如果你想比较 String 的值,你应该使用 Strings 的 Object.equals() 方法。

标签: android android-listview nullpointerexception


【解决方案1】:

1.可能适配器还没有设置listview

Handler handler = new Handler();        
    handler.postDelayed(new Runnable() {            
        public void run() {
              //your code
              CheckBox checkbox = (CheckBox)listView.getChildAt(j).findViewById(R.id.ColChk);
              //your code
        }
    }, 500);

2.也许你滚动列表视图所以索引是错误的,参考这个 Android ListView y position

【讨论】:

  • 例如,如果我从按钮小部件的 OnClickListener() 调用检查框是否正确检查的函数,但如果我从 getView() 调用它,则始终不检查复选框之一。然后我把计数器放在 getView 里面来计算有多少项目。如果我加载 3 个项目 getView 计数 2 但 listview 正确显示三个项目。
【解决方案2】:

尝试使用阵列适配器而不是基本适配器或类似的东西:

public class ArrayListAdapter<T> extends ArrayAdapter<T> {



     private List<T> arrayList;

        public ArrayListAdapter(Context context, int layout, List<T> arrayList) {
            super(context, layout);
            this.arrayList = arrayList;
        }

        @Override
        public T getItem(int position) {
            return arrayList.get(position);
        }

        @Override
        public int getCount() {
            return arrayList.size();
        }
    }

创建适配器实例并设置 listView.setAdapter(arrayAdapter); 我看到你没有提供 List 来调整它为什么你得到 nullPointerException

【讨论】:

  • BaseAdapter 可以和 list 一起使用,问题是 elsewere
【解决方案3】:

问题解决了。在 getView() 中我写了这几行,它只是我想要的:

for (k=0; k<rbmjere.size(); k++)
            {
                if (Chk.getTag().toString().equals(rbmjere.get(k).toString()))
                {
                    Chk.setChecked(true);

                }
            }   

感谢大家的帮助和想法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-19
    • 2019-07-18
    • 2013-08-12
    • 2015-03-27
    • 2019-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多