I'm just getting started in android app development and today I tried to tackle my first useful app, It's not anything fancy, just a contact manager that stores contact info and stores them in a listview but when I compile it, the app starts and even lets met switch between tabs (one tab has the input boxes and the other the listview) but after I input the contact, the next time I try and switch to the other tab, the app crashes and in the dialog box I can see the app threw a Null pointer exception error. Is there anyway that I can fix this in my by changing my codes syntax?
public void populatelist() {
ArrayAdapter<Contact> adapter = new contactlistAdapater() ;
contactlistview.setAdapter(adapter);
}
private void addcontact(String name, String phone, String email, String address){
contacts.add(new Contact(name, phone,email, address)) ;
}
private class contactlistAdapater extends ArrayAdapter<Contact> {
public contactlistAdapater() {
super (MainActivity.this, R.layout.contact_layout, contacts) ;
}
@Override
public View getView(int position, View view, ViewGroup parent ){
if (view == null)
getLayoutInflater().inflate(R.layout.contact_layout, parent, false) ;
Contact CurrentContact = contacts.get(position) ;
TextView name = (TextView) view.findViewById(R.id.name) ;
name.setText(CurrentContact.getname());
TextView phone = (TextView) view.findViewById(R.id.Phone) ;
phone.setText(CurrentContact.getphone());
TextView email = (TextView) view.findViewById(R.id.Email) ;
email.setText(CurrentContact.getemail());
TextView address = (TextView) view.findViewById((R.id.address)) ;
address.setText(CurrentContact.getaddress());
return view ;
}
}
findViewById is going to return null if it is given an invalid integer as its argument. So if you do not use the built in android studio autocomplete to create your R.id statements.... you are going to run into things like this.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.