-2

I have managed to access the contacts list to pick a contact and put number in the editText. The only problem I am facing is that the app stops when I press the back button while in the contact list. Can you please help?

Intent contactIntent = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
@Override
protected void onActivityResult(int reqCode, int resultCode, Intent intent) 
{
    super.onActivityResult(reqCode, resultCode, intent);
    switch (reqCode) {
    case ADDRESS_REQUEST2:
        EditText edittext2 = (EditText) findViewById(R.id.number2);
        Uri uri2 = intent.getData();
        Cursor cursor2 = getContentResolver().query(uri2, null, null, null, null);
        cursor2.moveToFirst();
        String phoneNumber2 = cursor2.getString(cursor2.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        edittext2.setText(phoneNumber2);
        break;
    case ADDRESS_REQUEST3:
        EditText edittext3 = (EditText) findViewById(R.id.number3);
        Uri uri3 = intent.getData();
        Cursor cursor3 = getContentResolver().query(uri3, null, null, null, null);
        cursor3.moveToFirst();
        String phoneNumber3 = cursor3.getString(cursor3.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        edittext3.setText(phoneNumber3);
        break;
    case ADDRESS_REQUEST4:
        EditText edittext4 = (EditText) findViewById(R.id.number4);
        Uri uri4 = intent.getData();
        Cursor cursor4 = getContentResolver().query(uri4, null, null, null, null);
        cursor4.moveToFirst();
        String phoneNumber4 = cursor4.getString(cursor4.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

        edittext4.setText(phoneNumber4);
        break;
    default:
        EditText edittext1 = (EditText) findViewById(R.id.number1);
        Uri uri1 = intent.getData();
        Cursor cursor1 = getContentResolver().query(uri1, null, null, null, null);
        cursor1.moveToFirst();
        String phoneNumber1 = cursor1.getString(cursor1.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
        edittext1.setText(phoneNumber1);
        break;
    }


}
superraf
  • 9
  • 4
  • Nothing to do with that question! We are talking about two different problems – superraf May 26 '14 at 14:26
  • 1
    if you read closely the question linked, it is not about a specific problem, it is about being able to debug a crash, or at least to be able to identify and collect the information needed for someone to debug the crash. – njzk2 May 26 '14 at 14:30
  • Can you put your logs please ? – Farouk Touzi May 26 '14 at 16:45

1 Answers1

0

You need to check for

if (resultCode == RESULT_OK) { ...

first of all.

When you exit the Contact Picker activity without selecting one, no Intent is returned, hence the NullPointerException. The resultCode value indicates whether the action was successful or not. As a general rule it will be RESULT_OK for a successful operation, and RESULT_CANCELED otherwise.

As a completely unrelated aside, you should refactor that method...

matiash
  • 52,725
  • 16
  • 117
  • 149