Nullpointerexception定制适配器在getview没有inflater

时间:2014-11-20 08:44:38

标签: java android nullpointerexception

每当我在我的Android应用程序中向下滚动时,我在第79行得到一个nullpointerexception 的 linear.addView(holder.rbg);
我尝试了几种解决方案,但它们似乎都没有用,因为我不使用inflater,因为我不知道它是如何工作的。 我可以为你提供我的XML文件,但其中唯一的内容是linearlayout。

 private class MyAdapter extends ArrayAdapter<String> {

        public MyAdapter(Context context, String[] strings) {
            super(context, -1, -1, strings);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {       //trying to reuse a recycled view
            ViewHolder holder;
            View v = convertView;
            LinearLayout linear = new LinearLayout(Bouwonderdeel.this);
            if (v == null) {
            holder = new ViewHolder();
           linear.setLayoutParams(new AbsListView.LayoutParams(
                    AbsListView.LayoutParams.WRAP_CONTENT,
                    AbsListView.LayoutParams.WRAP_CONTENT));
           linear.setId(50);
            holder.tv = new TextView(Bouwonderdeel.this);
            holder.tv.setId(100);

            holder.rbg = new RadioGroup(Bouwonderdeel.this);
            holder.rbg.setId(200);
            holder.rbg.setOrientation(LinearLayout.HORIZONTAL); 
            holder.rbg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                public void onCheckedChanged(RadioGroup group, int checkedId) {


                }
            });
            //Make the list Items containing radiobuttons en textviews.
            for (int i = 0; i < 5; i++) {
                        holder.rName = new RadioButton (Bouwonderdeel.this);
                        holder.rbg.addView(holder.rName);
            }  

            }
            else{
                holder = (ViewHolder) v.getTag();
            }

             linear.addView(holder.rbg);
             linear.addView(holder.tv);
             holder.tv.setText(super.getItem(position));

            return linear;

        }
    }
  static class ViewHolder{
        RadioButton rName;
        RadioGroup rbg;
        TextView tv;
        }

http://shrib.com/4RBAO1Ia

1 个答案:

答案 0 :(得分:0)

按此更改代码,

 private class MyAdapter extends ArrayAdapter<String> {

            public MyAdapter(Context context, String[] strings) {
                super(context, -1, -1, strings);
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {       //trying to reuse a recycled view
                ViewHolder holder;
    if(convertView == null){
                holder = new ViewHolder();
                LayoutInflater mInflater = (LayoutInflater)  context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = mInflater.inflate(R.layout.yourlayout, null);

    LinearLayout linear = (LinearLayout) convertView.findViewById(R.id.lnlayout);
    linear.setLayoutParams(new AbsListView.LayoutParams(
                        AbsListView.LayoutParams.WRAP_CONTENT,
                        AbsListView.LayoutParams.WRAP_CONTENT));
    holder.tv = (TextView) convertView.findViewById(R.id.lnlayout);
    holder.rbg = (RadioGroup) convertView.findViewById(R.id.lnlayout);
    convertView.setTag(holder);

    } else{
        holder = (ViewHolder) convertView.getTag()
    }



               linear.setId(50);

                holder.tv.setId(100);


                holder.rbg.setId(200);
                holder.rbg.setOrientation(LinearLayout.HORIZONTAL); 
                holder.rbg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    public void onCheckedChanged(RadioGroup group, int checkedId) {


                    }
                });
                //Make the list Items containing radiobuttons en textviews.
                for (int i = 0; i < 5; i++) {
                            holder.rName = new RadioButton (Bouwonderdeel.this);
                            holder.rbg.addView(holder.rName);
                }  

                }
                else{
                    holder = (ViewHolder) v.getTag();
                }

                 linear.addView(holder.rbg);
                 linear.addView(holder.tv);
                 holder.tv.setText(super.getItem(position));

                return convertView;

            }
        } 
hope it will help you