android.database.CursorIndexOutOfBoundsException: Index -1 requested

Solution 1:

After you execute query, you must call cur.moveToFirst()...

Try this

ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(Contacts.CONTENT_URI, 
       null, null, null, null);

if(cur != null && cur.moveToFirst())
{
       String id = cur.getString(cur.getColumnIndex(Contacts._ID));

        if (cur.getCount() > 0) {
         ...

Solution 2:

It is not required to call moveToFirst but to ensure that you moved to any of the returned rows before accessing a value (see moveToLast(), moveToPosition(int position), ... ).

So this one:

Cursor cur = cr.query(Contacts.CONTENT_URI, null, null, null, null);

while (cur.moveToNext()) {
    String id = cur.getString(cur.getColumnIndex(Contacts._ID));
    final Uri uri = RawContactsEntity.CONTENT_URI;

would work as well - or even better, because there is an issue with the accepted answer, if you continue with ...

while (cur.moveToNext()) {

after

if (cur.getCount() > 0) { 

It would skip the first row, what might not be obvious.