getChildAt上的Nullpointerexception

时间:2013-07-29 09:02:22

标签: android android-listview nullpointerexception

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

 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项而第3项缺失。如何检测列表视图的渲染完成后所有项目何时可见或如何检测?

3 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

1.可能适配器尚未设置列表视图

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

答案 2 :(得分:0)

问题解决了。在getView()里面我写了几行,它只是我想要的工作:

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

                }
            }   

感谢所有人的帮助和想法。

相关问题