0

大家好,我在准备联系人列表时遇到以下异常。

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

有什么问题?

4

4 回答 4

1

您需要先移动到第一列,以确保光标位于有效索引上:

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());
}
于 2012-04-13T02:10:50.830 回答
1

您可以使用以下方法获取联系人列表:

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;
}
于 2012-04-13T02:11:22.000 回答
0

我可以告诉你异常。这只是关于我没有转动您需要的行。您请求无效列

于 2012-04-13T02:12:39.237 回答
0

采用

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();

    }
}

当我遇到同样的问题时,这对我有什么帮助

在这里找到

于 2012-08-31T01:23:30.803 回答