我搜索了一些东西,但它没有在光标上显示红色标记。请更正这些错误。提前谢谢。这是正确的方法来获取完整的联系人姓名列表吗?
String address = (here its show error )cursor.getString(**cursor**.getColumnIndex("address"));
final String[] projection = new String[] {ContactsContract.Data.DISPLAY_NAME};
String displayName = null;
Cursor contactCursor = null;
try {
Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
Uri.encode(address));
contactCursor = getContentResolver().query(uri,
projection,
null,
null,
null);
if (contactCursor != null && contactCursor.moveToFirst()) {
displayName = contactCursor.getString(
contactCursor.getColumnIndex(ContactsContract.Data.DISPLAY_NAME));
}
}
catch(Exception e) {
e.printStackTrace();
}
finally {
if(contactCursor != null) {
contactCursor.close();
}
}
答案 0 :(得分:1)
如果您未设置阅读联系人的权限,请在<application/>
代码
<uses-permission android:name="android.permission.READ_CONTACTS" />
对于Android API 23+,您需要在尝试询问
之前将其添加到java类中requestpermissions(Manifest.android.permissions.READ_CONTACTS, 0/*int*/);