光标 - 确保在从中访问数据之前正确初始化Cursor

时间:2015-07-08 05:22:14

标签: android cursor contactscontract

我很少得到以下错误:

Couldn't read row 439, col 60 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
        at android.os.Parcel.readException(Parcel.java:1433)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:185)
        at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:137)
        at android.database.BulkCursorProxy.getWindow(BulkCursorNative.java:164)
        at android.database.BulkCursorToCursorAdaptor.onMove(BulkCursorToCursorAdaptor.java:83)
        at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:214)
        at android.database.AbstractCursor.moveToNext(AbstractCursor.java:245)
        at android.database.CursorWrapper.moveToNext(CursorWrapper.java:166)
        at ....UpdatePhoneContactsJob.h(UpdatePhoneContactsJob.java:64)

错误行对应while (cursor.moveToNext()) ...我使用以下函数:

Cursor cursor = MainApp.get().getContentResolver().query(
            ContactsContract.Data.CONTENT_URI,//ContactsContract.CommonDataKinds.Phone.CONTENT_URI
            null,
            null,//Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE + "'",
            null,
            ContactsContract.Data.CONTACT_ID + " ASC");

    int rawId = cursor.getColumnIndex(ContactsContract.Data.RAW_CONTACT_ID);
    int id = cursor.getColumnIndex(ContactsContract.Data.CONTACT_ID);
    int lookupKey = cursor.getColumnIndex(ContactsContract.Data.LOOKUP_KEY);
    // int photoUri = cursor.getColumnIndex(ContactsContract.Data.PHOTO_URI);
    int photoId = cursor.getColumnIndex(ContactsContract.Data.PHOTO_ID);
    int name = cursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME);
    int accountType = cursor.getColumnIndex(ContactsContract.Data.ACCOUNT_TYPE_AND_DATA_SET);
    int number = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    int numberType = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE);
    int numberLabel = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL);
    int mimeType = cursor.getColumnIndex(ContactsContract.Contacts.Data.MIMETYPE);
    int rawContactName = cursor.getColumnIndex(ContactsContract.RawContacts.ACCOUNT_NAME);

    ContactComparator compId = new ContactComparator(ContactComparator.Type.ID);
    ContactComparator compName = new ContactComparator(ContactComparator.Type.Name);

    while (cursor.moveToNext())
    {
        int phId = cursor.getInt(id);
        String phLookupKey = cursor.getString(lookupKey);
        // String phPhotoUri = cursor.getString(photoUri);
        int phPhotoId = cursor.getInt(photoId);
        long phRawId = cursor.getLong(rawId);
        String phNumber = cursor.getString(number);
        int phNumberType = cursor.getInt(numberType);
        String phNumberLabel = cursor.getString(numberLabel);
        String phMimeType = cursor.getString(mimeType);
        String phAccountType = cursor.getString(accountType);
        String phRawContactName = cursor.getString(rawContactName);

        PhoneContact contact = new PhoneContact(phId, phRawId, phLookupKey, phPhotoId > 0);
        // funktioniert, weil Kontakte nach ID sortiert sind!
        int contactIndex = Collections.binarySearch(contacts, contact, compId);
        if (contactIndex < 0)
        {
            String phName = cursor.getString(name);
            contact.setName(phName);
            contacts.add(contact);
        }
        else
        {
            contact = contacts.get(contactIndex);
        }

        String accountName = BaseDef.makeAccountTypeReadable(phAccountType, phRawContactName);
        contact.addAccountType(accountName);

        // Liste der AccountTypen merken
        if (!accountTypes.contains(accountName))
            accountTypes.add(accountName);

        if (phMimeType.equals(ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE))
        {
            String phoneLabel = (String) ContactsContract.CommonDataKinds.Phone.getTypeLabel(MainApp.get().getResources(), phNumberType, phNumberLabel);
            contact.addNumber(phoneLabel, phNumber);
        }
    }

    cursor.close();

此功能是否容易出错?我可以更改哪些内容以避免上述错误?

0 个答案:

没有答案