Search code examples
androidandroid-contactsandroid-cursor

Android Cursor error - Make sure cursor is initialised correctly


I've set up a Cursor which I want to return a listview of contacts who have a phone number. So, in each cell, I'll have the contact name and then phone number underneath. This code mostly does the job :

// this query only return contacts with phone number and is not duplicated
        phones = getContentResolver().query(
                ContactsContract.Contacts.CONTENT_URI,
                null,
//                we only want contacts that have a name and a phone number. If they have a phone number, the value is 1 (if not, it is 0)
                ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + ("1") + "'" + " AND " + ContactsContract.Contacts.HAS_PHONE_NUMBER + "=1",
                null,
                ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC");

And then when it comes to extracting information and putting it in each cell :

        while (phones.moveToNext()) {

            String name = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

I'm getting an error, part of the code which is:

04-07 10:43:46.489  17742-17760/com.example.chris.contactlistcustomlistview E/CursorWindow﹕ Failed to read row 0, column -1 from a CursorWindow which has 134 rows, 34 columns.
04-07 10:43:46.489  17742-17760/com.example.chris.contactlistcustomlistview W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0x416168e0)
04-07 10:43:46.489  17742-17760/com.example.chris.contactlistcustomlistview E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:299)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
            at java.util.concurrent.FutureTask.run(FutureTask.java:239)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
            at android.database.CursorWindow.nativeGetString(Native Method)

So, the cursor is hopping from Contacts.Contracts to ContactsContract.CommonDataKinds.Phone and it doesn't like it. But how else can I get the phone number of a user ? As far as I know, it's not in ContactsContracts.Contacts. Should I reinitialize the cursor or something before going to CommonDataKinds and how would I do this?


Solution

  • if (phones != null) {
        phones.moveToFirst();
    }