`Adapter class :::
public class YAdapter extends ArrayAdapter<String> {
ArrayList<String> items;
Context context;
private LayoutInflater mInflater;
static class ViewHolder {
TextView yName;
}
public YAdapter(Context context,ArrayList<String> items) {
super(context, R.layout.single_item_list_row, items);
this.context=context;
mInflater = LayoutInflater.from(context);
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public String getItem(int arg0) {
return items.get(arg0);
}
@Override
public long getItemId(int arg0) {
return arg0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if(convertView == null) {
convertView = Inflater.inflate(R.layout.single_item_list_row,null);
holder = new ViewHolder();
holder.yName=(TextView)convertView.findViewById(R.id.text1);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
try
{
if(items.get(position) != null)
holder.yName.setText(items.get(position));
}
catch (Exception e) {
Log.e("ERROR","Adapter error in generating view);
}
return convertView;
}
}
Activity Class ::
public class DemoActivity extends Activity {
ListView listView;
ArrayList<String> temp = new ArrayList<String>();
YAdapter adapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
listView = (ListView) findViewById(R.id.list);
}
@Override
public void onResume() {
super.onResume();
temp.add("one");
temp.add("two");
temp.add("three");
temp.add("four");
adapter = new YAdapter(DemoActivity.this , temp);
**listView.setAdapter(adapter);** -- exception
}
}
listView.setAdapter(adapter);
this line causes an exception.
当我尝试将适配器设置为列表时,如果我删除此行,则不会显示错误,并且屏幕上也没有显示任何组件,只会出现黑屏。
我编码的内容如下所示bt我得到空指针异常。请告诉我我错过了什么?