0

Alright, so the book I'm going through wants me to call a specific person.

I can retrieve the contact just fine, it's the whole "getting contact ID to find the phone number bit" that I'm having trouble with.

The app is crashing every time I select a contact.

I suppose I'm not sure how to properly navigate to the contact ID.

I am fairly new to Android and now I'm just getting completely lost, I haven't be able to find a solution on here that helps me.

Relevant Code:

   }else if(requestCode == REQUEST_CONTACT) {
        Uri contactsURI = data.getData();

        String[] queryFields = new String[]{
                ContactsContract.Contacts.DISPLAY_NAME
        };


        //Perform your query - the contactURI is like a "where"
        //clause here
        Cursor c = getActivity().getContentResolver().query(contactsURI, queryFields, null, null, null);


        //Double-check that you actually got results
        if (c.getCount() == 0) {
            c.close();
            return;
        }
        contactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));

        //Pull out the first column of the first row of data
        //that is your suspects name
        c.moveToFirst();

        String suspect = c.getString(0);


        mCrime.setmSuspect(suspect);
        mSuspectButton.setText(suspect);
        c.close();

    }



}

I am crashing because of this part of the above code:

contactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
MrTimotheos
  • 937
  • 1
  • 10
  • 18

1 Answers1

0

You have to first call

c.moveToFirst();

Then

contactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID))
Mohammed Ali
  • 2,758
  • 5
  • 23
  • 41