Okay So I create a ListView and my own ListAdapter but whenever I add a result to the ListAdapter it crashes saying NullPointerException
I do start the Adaptor when the filldata() is opened MyAdapter adapter = new MyAdapter ();
My Code:
public void filldata() throws IOException{
MyAdapter adapter = new MyAdapter ();
for (int i = 1; i <= 10; i++) {
AdapterItem test = new AdapterItem(1+i, 2+i, 3+i);
adapter.addAdapterItem(test);
}
setListAdapter(adapter);
}
}
public class MyAdapter extends BaseAdapter {
private List<AdapterItem> items;
public void addAdapterItem(AdapterItem item) {
items.add(item);
}
public int getCount() {
return items.size();
}
public Object getItem(int position) {
return items.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent){
View rowView = getLayoutInflater().inflate(R.layout.searchitemview, parent);
TextView firstTextView = (TextView) rowView.findViewById(R.id.txtTitle);
firstTextView.setText(items.get(position).first);
// do the same with second and third
return rowView;
}
}
class AdapterItem {
public String first;
public String second;
public String third;
public AdapterItem(String first, String second, String third) {
this.first = first;
this.second = second;
this.third = third;
}
}
When it gets to adapter.addAdapterItem(test); it crashes
Change here.
You’re not instantiating
itemin theMyAdapterclass.