Search code examples
androidandroid-contentresolver

getting illegal state exception while retrieving contacts


When i am retrieving contacts, it gives illegal state exception..

here is my code..

private ArrayList<HashMap<String, String>> getContacts() {
    // TODO Auto-generated method stub
    Uri uri = ContactsContract.CommonDataKinds.Phone.CONTENT_URI;
    String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.NUMBER};

    Cursor people = getContentResolver().query(uri, projection, null, null, null);

    int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    int numberType = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
    if(people.getCount()>0){
    while (people.moveToNext())
     {
        // For Number Type
        String nameType="";
        int type = people.getInt(numberType);
        switch (type) {
            case Phone.TYPE_HOME:
                    nameType= "_Home";
                break;
            case Phone.TYPE_MOBILE:
                nameType= "_Mobile";
                break;
            case Phone.TYPE_WORK:
                nameType= "_Work";
                break;
            case Phone.TYPE_OTHER:
                nameType= "_Other";
                break;
            }

        String name   = people.getString(indexName)+""+nameType;
        String number = people.getString(indexNumber);
        System.out.println("name: "+name+" number: "+number);
        // Do work...
        HashMap<String, String> namePhoneType = new HashMap<String, String>();

        namePhoneType.put(name, number);
        System.out.println("Hash map: "+namePhoneType);
        //namePhoneType.put("Name", name);
        //namePhoneType.put("Phone", number);
        // adding to array list
        contactList.add(namePhoneType);
        System.out.println("Array list: "+contactList);
    } 
    }
    return contactList;
}

here my logs...

01-27 21:10:29.290: E/AndroidRuntime(333): FATAL EXCEPTION: main
01-27 21:10:29.290: E/AndroidRuntime(333): java.lang.RuntimeException: Unable to start activityComponentInfo{com.first/com.first.SecondActivity}: java.lang.IllegalStateException: get fieldslot from row 0 col -1 failed
01-27 21:10:29.290: E/AndroidRuntime(333):  atandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-27 21:10:29.290: E/AndroidRuntime(333):  atandroid.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-27 21:10:29.290: E/AndroidRuntime(333):  atandroid.app.ActivityThread.access$1500(ActivityThread.java:117)
01-27 21:10:29.290: E/AndroidRuntime(333):  atandroid.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-27 21:10:29.290: E/AndroidRuntime(333):  atandroid.os.Handler.dispatchMessage(Handler.java:99)
01-27 21:10:29.290: E/AndroidRuntime(333):  at android.os.Looper.loop(Looper.java:123)
01-27 21:10:29.290: E/AndroidRuntime(333):  atandroid.app.ActivityThread.main(ActivityThread.java:3683)
01-27 21:10:29.290: E/AndroidRuntime(333):  at java.lang.reflect.Method.invokeNative(Native Method)
01-27 21:10:29.290: E/AndroidRuntime(333):  at java.lang.reflect.Method.invoke(Method.java:507)
01-27 21:10:29.290: E/AndroidRuntime(333):  atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-27 21:10:29.290: E/AndroidRuntime(333):  atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-27 21:10:29.290: E/AndroidRuntime(333):  at dalvik.system.NativeStart.main(Native Method)
01-27 21:10:29.290: E/AndroidRuntime(333): Caused by: java.lang.IllegalStateException: get fieldslot from row 0 col -1 failed
01-27 21:10:29.290: E/AndroidRuntime(333):  atandroid.database.CursorWindow.getLong_native(Native Method)
01-27 21:10:29.290: E/AndroidRuntime(333):  atandroid.database.CursorWindow.getInt(CursorWindow.java:434)
01-27 21:10:29.290: E/AndroidRuntime(333):  atandroid.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:93)
01-27 21:10:29.290: E/AndroidRuntime(333):  atandroid.database.CursorWrapper.getInt(CursorWrapper.java:123)
01-27 21:10:29.290: E/AndroidRuntime(333):  atcom.first.SecondActivity.getContacts(SecondActivity.java:78)
01-27 21:10:29.290: E/AndroidRuntime(333):  atcom.first.SecondActivity.onCreate(SecondActivity.java:59)
01-27 21:10:29.290: E/AndroidRuntime(333):  atandroid.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
01-27 21:10:29.290: E/AndroidRuntime(333):  atandroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

Solution

  • Look at the error code here:

    String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME,
                    ContactsContract.CommonDataKinds.Phone.NUMBER};
    
    Cursor people = getContentResolver().query(uri, projection, null, null, null);
    
    int indexName = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    int indexNumber = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    int numberType = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
    

    In first 2 lines, you got an Cursor: people which contains 2 fields: DISPLAY_NAME and NUMBER. After quering, you tried to get TYPE and I returned -1, becuause your cursor contains only 2 fields (not TYPE), so that's your error.

    P/s: after querying, you should use cursor.moveToFirst() function. Why? Look at Android Cursor link