android.database.CursorIndexOutOfBoundsException: Index -1 requested
<p>I'm trying to read all the contacts using RawContacts.entityIterator but I'm seeing this error:</p>

<p>android.database.CursorIndexOutOfBoundsException: Index -1 requested</p>

<p>Following is my code:</p>

<pre><code>ContentResolver cr = getContentResolver();
        Cursor cur = cr.query(Contacts.CONTENT_URI, 
       null, null, null, null);

String id = cur.getString(cur.getColumnIndex(Contacts._ID));

if (cur.getCount() &gt; 0) {
        while (cur.moveToNext()) {
         final Uri uri = RawContactsEntity.CONTENT_URI;
            final String selection = Data.CONTACT_ID + "=?";
            final String[] selectionArgs = new String[] {id};

            final Map&lt;String, List&lt;ContentValues&gt;&gt; contentValuesListMap =
                new HashMap&lt;String, List&lt;ContentValues&gt;&gt;();
            EntityIterator entityIterator = null;

            entityIterator = RawContacts.newEntityIterator(cr.query(
                    uri, null, selection, selectionArgs, null));

            while (entityIterator.hasNext()) {
                Entity entity = entityIterator.next();
                for (NamedContentValues namedContentValues : entity.getSubValues()) {
                    ContentValues contentValues = namedContentValues.values;
                    String key = contentValues.getAsString(Data.MIMETYPE);
                    if (key != null) {
                        List&lt;ContentValues&gt; contentValuesList =
                                contentValuesListMap.get(key);
                        if (contentValuesList == null) {
                            contentValuesList = new ArrayList&lt;ContentValues&gt;();
                            contentValuesListMap.put(key, contentValuesList);
                        }
                        contentValuesList.add(contentValues);
                    }
                }
            }
            Log.i(tag, "Contact index=" + id);
        } 
    }
</code></pre>

<p>Can somebody please tell me what's wrong with my code.</p>
