Исключение при чтении списка контактов

Привет, ребята, я получаю следующее исключение при подготовке списка контактов.

04-13 13:51:15.210: E/AndroidRuntime(7343): java.lang.IllegalStateException: get field slot from row 0 col -1 failed

Вот моя функция getContact

 public static ContactList getContactList(Context context){
    ContactList contactList = new ContactList(RequestStatus.CONTACT_LIST);
    Cursor people =     context.getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
            null, null, null, null);
    while(people.moveToNext()) {
       int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
       String contact = people.getString(nameFieldColumnIndex);
       int numberFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER);
       String number = people.getString(numberFieldColumnIndex);  
       contactList.addContact(new Contact(contact,number));
    }
    people.close();
    return contactList;
}

Исключение выдается в следующей строке.

String number = people.getString(numberFieldColumnIndex); 

что может быть не так?


person Raju Kumar    schedule 13.04.2012    source источник
comment
col -1 не удалось - похоже, что people.getColumnIndex(PhoneLookup.DISPLAY_NAME) вернул -1.   -  person Steve Wong    schedule 13.04.2012


Ответы (4)


Вы можете использовать следующий метод, чтобы получить список контактов:

private ContactList getDetails(){
    ContactList contactList = new ContactList(RequestStatus.CONTACT_LIST);
    Uri uri = contactsContract.CommonDataKinds.Phone.CONTENT_URI;
    ContentResolver cr = getContentResolver();
    Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
    String[] projection    = new String[] {ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER };
    Cursor names = getContentResolver().query(uri, projection, null, null, null);

    int indexName = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
    int indexNumber = names.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
    names.moveToFirst();
    do {
        String name   = names.getString(indexName);
        Log.e("Name new:", name);
        String number = names.getString(indexNumber);
        Log.e("Number new:","::"+number);
        contactList.addContact(new Contact(name,number));
    } while (names.moveToNext());
    return contactList;
}
person Shankar Agarwal    schedule 13.04.2012

Вам нужно перейти к первому столбцу раньше, чтобы убедиться, что курсор находится на действительном индексе:

if (people.moveToFirst()) {
    do {
       int nameFieldColumnIndex = people.getColumnIndex(PhoneLookup.DISPLAY_NAME);
       String contact = people.getString(nameFieldColumnIndex);
       int numberFieldColumnIndex = people.getColumnIndex(PhoneLookup.NUMBER);
       String number = people.getString(numberFieldColumnIndex);  
       contactList.addContact(new Contact(contact,number));
    }while(people.moveToNext());
}
person MByD    schedule 13.04.2012

Я могу рассказать вам об исключении. Это было как раз о том, что я не перевернул строку, которую вы требовали. либо вы запрашиваете неверный столбец

person coading fever    schedule 13.04.2012

использовать

public static void getContactNumbers(Context context) {
    String contactNumber = null;
    int contactNumberType = Phone.TYPE_MOBILE;
    String nameOfContact = null;
    if (ApplicationConstants.phoneContacts.size() <= 0) {
        ContentResolver cr = context.getContentResolver();
        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
                null, null, null);
        if (cur.getCount() > 0) {
            while (cur.moveToNext()) {
                String id = cur.getString(cur
                        .getColumnIndex(BaseColumns._ID));
                nameOfContact = cur
                        .getString(cur
                                .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                if (Integer
                        .parseInt(cur.getString(cur
                                .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {
                    Cursor phones = cr
                            .query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                                    null,
                                    ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                            + " = ?", new String[] { id },
                                    null);

                    while (phones.moveToNext()) {
                        contactNumber = phones.getString(phones
                                .getColumnIndex(Phone.NUMBER));
                        contactNumberType = phones.getInt(phones
                                .getColumnIndex(Phone.TYPE));
                        Log.i(TAG, "...Contact Name ...." + nameOfContact
                                + "...contact Number..." + contactNumber);

                    }
                    phones.close();
                }

            }
        }// end of contact name cursor
        cur.close();

    }
}

вот что помогло мне, когда у меня была такая же проблема

нашел здесь

person Asaf Manassen    schedule 31.08.2012