Android 读取联系人列表时发生异常

Android 读取联系人列表时发生异常,android,android-contacts,Android,Android Contacts,大家好,我在准备联系人列表时遇到了以下异常 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

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

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

可能有什么问题?

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

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

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

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

我可以告诉你例外情况。这只是因为我没有按你的要求排好队。您请求的列无效

使用

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

    }
}
public static void getcontactnumber(上下文){
字符串contactNumber=null;
int contactNumberType=Phone.TYPE\u MOBILE;
字符串nameOfContact=null;
if(ApplicationConstants.phoneContacts.size()0){
while(cur.moveToNext()){
字符串id=cur.getString(cur
.getColumnIndex(BaseColumns._ID));
合同名称=cur
.getString(cur)
.getColumnIndex(Contacts contract.Contacts.DISPLAY_NAME));
if(整数
.parseInt(cur.getString(cur
.getColumnIndex(Contacts contract.Contacts.HAS_PHONE_NUMBER))>0){
光标=cr
.query(contacts contract.commondatatypes.Phone.CONTENT\u URI、,
无效的
Contacts contract.CommonDataTypes.Phone.CONTACT\u ID
+“=?”,新字符串[]{id},
无效);
while(phones.moveToNext()){
contactNumber=phones.getString(电话
.getColumnIndex(电话号码));
contactNumberType=phones.getInt(电话
.getColumnIndex(Phone.TYPE));
Log.i(标记“…联系人姓名…”+联系人姓名
+“…联系电话…”+联系电话);
}
电话。关闭();
}
}
}//联系人名称结束光标
cur.close();
}
}
当我遇到同样的问题时,这对我有帮助

“列-1失败”-看起来像人。getColumnIndex(PhoneLookup.DISPLAY\u NAME)返回-1。