`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我得到空指针异常。请告诉我我错过了什么?
答案 0 :(得分:2)
查看此行,您的活动名称
public class Activity extends Activity {
并在适配器行
adapter = new YAdapter(YellowPagesActivity.this , temp);
那么什么是 YellowPagesActivity.this ?
答案 1 :(得分:0)
使用@ViewById(R.id.list) so no need to findViewById
.....
问题到期
adapter = new YAdapter(YellowPagesActivity.this , temp);