我从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
。
如何让这个代码段有效?