0

I created an onActivityResult method. I am in the contact list, Upon clicking I should get the name and the phone number of the contact, but it crashes.

  @Override
   protected  void onActivityResult(int reqCode, int resultCode, Intent data) {
    //TODO Auto-generated method stub
    super.onActivityResult(reqCode, resultCode, data);



    if (reqCode == PICK_CONTACT) {
        if(resultCode == ActionBarActivity.RESULT_OK){
            Uri contactData = data.getData();
            Cursor c = getContentResolver().query(contactData, null, null, null, null);

            if(c.moveToFirst()) {
                Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);  
                startActivityForResult(intent, PICK_CONTACT);
                String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
               String phone = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                Toast.makeText(this, "You've picked" + name, Toast.LENGTH_SHORT).show();

            }
        }
    }
}

Please note, if I remove the line

     String phone = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

it doesn't crash. (But I don't get the phone number.

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Armin Beda
  • 109
  • 9
  • What code here calls anything? You start the Pick activity twice – OneCricketeer Feb 03 '17 at 07:42
  • `ContactsContract.CommonDataKinds.Phone.NUMBER` is a constant value. Your toast will never change – OneCricketeer Feb 03 '17 at 07:43
  • and what is the solution to get the number and not a constant value, @cricket_007? – Armin Beda Feb 03 '17 at 08:01
  • You have `c.moveToFirst()`. Hint: why did you do that? What are you using the Cursor for? Your question doesn't extract anything from the cursor – OneCricketeer Feb 03 '17 at 08:09
  • the contacts are open. I would like to get the number upon clicking, therefore I am using cusror. For example the name I can get, but the number I cannot. The reason I pick acitivity twice is that it 'stays' in the contact, so the user can choose more contacts. Can you tell me why I can get the name and why I cannot the phonenumber using the method above? – Armin Beda Feb 03 '17 at 08:38
  • or @cricket_007 let's look at it that way: – Armin Beda Feb 03 '17 at 08:50
  • I have a button: – Armin Beda Feb 03 '17 at 08:51
  • Can you please [edit] your question with that code so it can be formatted correctly? – OneCricketeer Feb 03 '17 at 13:41
  • Also, what do you mean 'it doesn't crash'? If your app crashes, then please add the logcat with the error message. You are telling it to throw an error with `getColumnIndexOrThrow`, so are you not try-catching that? Please show a [mcve] of the problem - I don't see you extracting any data from that Cursor in your initial question – OneCricketeer Feb 03 '17 at 13:44
  • 1
    I found this post - seems to do what you want and more. You do need a manifest permission to read contacts, so maybe that's your problem http://stackoverflow.com/a/8182638/2308683 – OneCricketeer Feb 03 '17 at 13:53

0 Answers0