我写了一个程序,其中我有一个按钮,我想显示点击按钮时的年份列表,我总是在按钮的onclick方法上出现错误(空指针异常),我是还张贴了我希望我的列表看起来的图片,我有完全相同的按钮,请告诉我我哪里出错了
下面是我的代码
public class Result_Date extends Activity {
ImageView iv;
String[] years = {"2000", "2001", "2002", "2003", "2004",
"2005", "2006", "2007", "2008", "2009", "2010", "2011", "2012",
"2013", "2014" };
CustomAdapter customAdapter;
Button buttonShowYears;
AdapterNew adapter;
ListView listShowYear;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.result_list_year);
buttonShowYears = (Button) findViewById(R.id.btnShowYear);
listShowYear = (ListView) findViewById(R.id.listView1);
buttonShowYears.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
customAdapter = new CustomAdapter(getBaseContext(),
R.layout.row, years);
//also tried with the android.R.layout.simple_list_item_1 but not working
listShowYear.setAdapter(customAdapter);
}
});
我的CustomAdapter.java
public class CustomAdapter extends ArrayAdapter<String> {
Context context;
ArrayList<String> arrayList;
String[] years;
public CustomAdapter(Context context, int resource, String[] years) {
super(context, resource, years);
this.context = context;
this.years = years;
}
public static class ViewHolder {
TextView txtname;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
LayoutInflater vi = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.row, null);
holder = new ViewHolder();
holder.txtname = (TextView) convertView.findViewById(R.id.txtName);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.txtname.setText(years.length);
return convertView;
}