Reputation: 176
i have written a program in which i have a button and i want to display a list of years when clicked on the button , i am always getting an error on the onclick method of the button(null pointer exception) , i am also posting a picture of how i want my list to look , i have exactly the same button and please suggest me where i am going wrong
below is my code
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);
}
});
My 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;
}
Upvotes: 0
Views: 1280