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;
}
}