カスタムアレイアダプタを使用して連絡先のリストを表示しようとしています。ただし、アプリケーションを実行するたびに、次のエラーが発生します。
com.example.contactsapp.ContactsListAdapter.getView(ContactsListAdapter.java:37)でのjava.lang.NullPointerException
これは私のカスタムリストアダプタクラスです:
public class ContactsListAdapter extends ArrayAdapter<Contact> {
List <Contact> people;
//Contact c;
TextView name;
TextView email;
public ContactsListAdapter(Context context, List<Contact> people) {
super(context, R.layout.list_row, people);
this.people = people;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null){
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.list_row, null);
}
Contact c = this.people.get(position);
name = (TextView)v.findViewById(R.id.namebox);
name.setText(c.getName());
email = (TextView)v.findViewById(R.id.emailbox);
email.setText(c.getEmail());
return v;
}
}
これは私のListViewアクティビティです。
public class ViewContacts extends ListActivity{
List <Contact> people;
ContactsListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_view);
createTestData();
adapter = new ContactsListAdapter(this, people);
this.setListAdapter(adapter);
}
public void createTestData(){
people = new ArrayList<Contact>();
people.add(new Contact("Jack", "jc@hotmail.com"));
people.add(new Contact("Jack", "jc@hotmail.com"));
people.add(new Contact("Jack", "jc@hotmail.com"));
people.add(new Contact("Jack", "jc@hotmail.com"));
people.add(new Contact("Jack", "jc@hotmail.com"));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
Contact c = (Contact)people.get(position);
Toast.makeText(v.getContext(), c.getName().toString() + " Clicked!", Toast.LENGTH_SHORT).show();
}
}
何か案は?