0

I am trying contact screen and when user pick contact from there it's name and phone number will display in Toast message in onActivityResult screen. The problem is that when I pick the contact it's name is displaying in the toast when I am trying to display number error occurring:

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

Here is my code of onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    if(resultCode==Activity.RESULT_OK){
        switch(requestCode){
        case 1:
            Cursor c=managedQuery(data.getData(), null, null, null, null);              
            if(c.moveToFirst()){
                try{
                String name=c.getString(c.getColumnIndex(People.NAME));
                String no=c.getString(c.getColumnIndex(People.NUMBER));
                Toast.makeText(getApplicationContext(), no, 1).show();
                }
                catch(Exception e){
                    Log.v("error", e.toString());
                }
            }
            break;
        }           
    }
}
Alec.
  • 5,371
  • 5
  • 34
  • 69

1 Answers1

0

Please try this below mentioned code will help you out...

`Uri contactdate = data.getData();
                Cursor cursor = managedQuery(contactdate, null, null, null,null);
                if(cursor.moveToFirst()){
                    String id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
                    String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                    if(hasPhone.equalsIgnoreCase("1")){
                        Cursor phones = getContentResolver().query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id, null,null);
                        phones.moveToFirst();
                        String phoneNumebr = phones.getString(phones.getColumnIndex("data1"));
                        String mFormatedPhoneNumber = phoneNumebr.replace("-", "").replace(" ", "").replace("(","").replace(")", "");
                        Log.i(TAG, "Phone Number :"+mFormatedPhoneNumber);                      

                }else{
                    Log.i(TAG, "Phone Number Not available");
                }
                Log.i(TAG, "Name :"+cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)));
            }`
Srinivasan
  • 4,481
  • 3
  • 28
  • 36