阅读Android联系人时出错

时间:2015-04-11 05:50:31

标签: android contacts

我从StackOverflow获得了这个片段:

Cursor people = getContentResolver().query(
            ContactsContract.Contacts.CONTENT_URI, null, null, null, null);

    try{
        while (people.moveToNext()) {
            int nameFieldColumnIndex = people
                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME);
            String contact = people.getString(nameFieldColumnIndex);
            int numberFieldColumnIndex = people.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);
            String number =  people.getString(numberFieldColumnIndex);

            System.out.println(contact + "-" + number);
        }
    }catch(Exception e){
        System.out.println(e);
    }
    people.close();

当我尝试阅读phoneNum列时,我使用String number = people.getString(numberFieldColumnIndex)收到错误消息。检查列索引我发现numberFiledColomnIndex = -1

如何让这个代码段有效?

1 个答案:

答案 0 :(得分:0)

根据documentation,当列不存在时,getColumnIndex()返回-1。

问:您是否传递了正确的列名?拼写是否正确?