YaWotMate YaWotMate - 3 months ago 17
Android Question

Storing Contact Name in String

I keep getting the following crash when I try to store the contacts name I selected from the Contact Book into a TextView. I'm not sure what's wrong here as the Toast shows the contacts name stored in the string variable 'name' but when I try to TextView.setText(name) I get a null pointer exception?

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/0r1-4F314D4F514D314B454331/1 flg=0x1 }} to activity {com.example.calvin.pickmeup/com.example.calvin.pickmeup.Home}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(int)' on a null object reference


Home:

private TextView Address;
private TextView selectedContact;
private String name;
private final int PICK_CONTACT = 1;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);

Button currentLocation = (Button) findViewById(R.id.currentLocation);
Button chooseTime = (Button) findViewById(R.id.chooseTime);
Button chooseContact = (Button) findViewById(R.id.chooseContact);
TextView Address = (TextView) findViewById(R.id.Address);
TextView selectedContact = (TextView) findViewById(R.id.selectedContact);
final TextView selectedTime = (TextView) findViewById(R.id.selectedTime);

currentLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Home.this, MapsActivity.class);
startActivity(intent);
}
});

chooseTime.setOnClickListener(new View.OnClickListener(){

@Override
public void onClick(View v) {
Calendar mcurrentTime = Calendar.getInstance();
int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
int minute = mcurrentTime.get(Calendar.MINUTE);
TimePickerDialog mTimePicker;
mTimePicker = new TimePickerDialog(Home.this, new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
selectedTime.setText( selectedHour + ":" + selectedMinute);
}
}, hour, minute, true);//Yes 24 hour time
mTimePicker.setTitle("Select Time");
mTimePicker.show();
}
});

Bundle extras = getIntent().getExtras();
if(extras != null) {
String value = extras.getString("address");
Address.setText(value);
}
}

public void callContacts(View v){

Intent showContacts = new Intent (Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(showContacts,PICK_CONTACT);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if(requestCode == PICK_CONTACT) {
if(resultCode == ActionBarActivity.RESULT_OK){
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null,null,null,null);
if (c.moveToFirst()){
name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY));
Toast.makeText(this,"You've Picked:" + name, Toast.LENGTH_LONG).show();
selectedContact.setText(name);
}
}
}

}


The problem is with:

selectedContact.setText(name);

Answer

You are defining a global variable:

private TextView Address;
private TextView selectedContact;

Change this code:

TextView Address = (TextView) findViewById(R.id.Address);
TextView selectedContact = (TextView) findViewById(R.id.selectedContact);

and asign this values to defined global variables:

Address = (TextView) findViewById(R.id.Address);
selectedContact = (TextView) findViewById(R.id.selectedContact);
Comments