Whenever I scroll down in my android app, I get a nullpointerexception on line 79 linear.addView(holder.rbg);
I tried several solutions but none of them seem to work, since I don't use an inflater because I have no idea how it works. I can provide you my XML file but the only thing inside it is a 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;
}
Change you code by this,
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.