好的,所以我创建了一个ListView和自己的ListAdapter
,但是每当我将结果添加到ListAdapter
时,它都会崩溃并显示NullPointerException
Adaptor
打开时,我确实启动了filldata()
我的代码:
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;
}
}
到达
MyAdapter adapter = new MyAdapter ();
时崩溃 最佳答案
在这里换。
public class MyAdapter extends BaseAdapter
{
private List<AdapterItem> items=new ArrayList<AdapterItem>();
}
您没有在
item
类中实例化MyAdapter
。关于java - 带有ListAdapter的奇怪NullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8636743/