我正在做一个需要列出所有联系人的项目。我正在关注这篇文章。
我的AndroidManifest.xml包含以下内容,因此我可以阅读联系人:
<uses-permission android:name="android.permission.READ_CONTACTS"/>
代码在这里:
private void getContacts() {
try {
// Form an array specifying which columns to return.
String[] projection = new String[] {
People._ID,
People._COUNT,
People.NAME,
People.NUMBER};
// Get the base URI for the People table in the Contacts content provider.
Uri contacts = People.CONTENT_URI;
// Make the query.
Cursor managedCursor = managedQuery(contacts,
projection, // Which columns to return
null, // Which rows to return (all rows)
null, // Selection arguments (none)
// Put the results in ascending order by name
People.NAME + " ASC");
printContacts(managedCursor);
}
catch(Exception ex) {
Log.d("Contacts",ex.toString());
}
}
private void printContacts(Cursor cur){
if (cur.moveToFirst()) {
String name;
String phoneNumber;
int nameColumn = cur.getColumnIndex(People.NAME);
int phoneColumn = cur.getColumnIndex(People.NUMBER);
String imagePath;
do {
// Get the field values
name = cur.getString(nameColumn);
phoneNumber = cur.getString(phoneColumn);
Log.d("Contacts","Name: "+ name + " **** Phone: "+ phoneNumber);
} while (cur.moveToNext());
}
}
当我在 Emulator(2.3.3) 上运行它时,它会抛出以下错误:
java.lang.IllegalArgumentException: Invalid column _count
有人可以修复它吗?非常感谢您宝贵的时间和帮助。