Android游标错误 - 确保游标已正确初始化

时间:2016-04-07 10:10:31

标签: android android-contacts android-cursor

我已经设置了一个Cursor,我想返回一个有电话号码的联系人的列表视图。因此,在每个单元格中,我将在下面显示联系人姓名和电话号码。这段代码主要完成这项工作:

// 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");

然后在提取信息并将其放入每个单元格时:

        while (phones.moveToNext()) {

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

我收到错误,代码的一部分是:

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)

因此,光标从Contacts.Contracts跳到ContactsContract.CommonDataKinds.Phone并且它不喜欢它。但我怎样才能获得用户的电话号码?据我所知,它不在ContactsContracts.Contacts。我应该在转到CommonDataKinds之前重新初始化光标或其他内容,我该怎么做?

2 个答案:

答案 0 :(得分:1)

Have you tried calling phones.moveToFirst() before phones.moveToNext()? It moves to the first row. This is because your current query returns a Cursor object positioned before the first entry.

You can try writing:

if (phones.moveToFirst()) {

       while (phones.moveToNext()) {
                    // etc

答案 1 :(得分:0)

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