0

First of all sorry for my Bad English i'm new To Android development. I'm trying to develop this App in which user can click on a button and the Contacts will show up and then can pick one and only one one of the Contacts and show its number in a textbox. But i face this Error every time :

"Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it"

and The Funny thing is when i use HAS_PHONE_NUMBER it returns 1 which means the Contact has Phone Number. and when i use DISPLAY_NAME it returns contact name without any problem. but when i use NUMBER that Error shows Up! can anyone help me? BTW i use Fragment if it helps..? and this is my code :

@Override
public void onClick(View v) {

    if (v.getId() == R.id.btnContact) {

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

}


private Uri uriContact;
private String contactID; // contacts unique ID





@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
    super.onActivityResult(reqCode, resultCode, data);

    switch (reqCode) {
        case (PICK_CONTACT):
            if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                Cursor phone = getActivity().getContentResolver().query(contactData, null, null, null, null);
                phone.moveToFirst();

                if (phone.moveToFirst()) {
                    String num = phone.getString(phone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
                    // Todo something when contact number selected
                    placeholderNumber.setText("" + num);
                }
            }
            break;
    }
}
islamdidarmd
  • 725
  • 5
  • 19
  • interesting, this is the second time *today* that i answer this exact question. you should use a "phone picker" not a "contact picker" for this use-case, see my answer here: https://stackoverflow.com/a/47271920/819355 – marmor Nov 14 '17 at 11:12
  • Possible duplicate of [How to import a Specific Contact's phone number?](https://stackoverflow.com/questions/47270941/how-to-import-a-specific-contacts-phone-number) – marmor Nov 14 '17 at 11:12
  • @marmor tnx Bro thats really Funny Exaactly some One asked the same Question as mine at the Same Time :))))) tnx BTW – benyamin mokhtarpour Nov 14 '17 at 12:16
  • sure thing, can you please then confirm this question is a duplicate and close it, so it'll keep StackOverflow nice and clean? – marmor Nov 14 '17 at 19:36

0 Answers0