Android java.lang.IllegalArgumentException:检索联系人时出现无效的列_count错误

发布于 2024-12-21 08:34:00 字数 2156 浏览 2 评论 0原文

我正在开展一个项目,我需要列出所有联系人。我正在关注这篇文章

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

当我在模拟器(2.3.3)上运行它时,它会抛出以下错误:

java.lang.IllegalArgumentException: Invalid column _count

可以有人修复它吗? 非常感谢您的宝贵时间和帮助。

I am working on a project where I need to list down all the contacts. I am following this article.

My AndroidManifest.xml contains below, so i can read the contacts:

<uses-permission android:name="android.permission.READ_CONTACTS"/>

Code is here:

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

When I run it on the Emulator(2.3.3) it throws below error:

java.lang.IllegalArgumentException: Invalid column _count

Can someone fix it?
Great thanks for your valuable time & help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

铁憨憨 2024-12-28 08:34:00

如果删除字符串 People._COUNT 一切正常

另请参阅:Android SDK - 列出所有用户

If you remove the string People._COUNT everything works

See also: Android SDK - List All Users

无需解释 2024-12-28 08:34:00

Android People 类已弃用。您应该使用 ContactsContract 代替。
由于您在模拟器 API 级别 10 上运行它,并且该类自 API 级别 5 起已被弃用,因此没有理由继续使用 People。

Android People class is deprecated. You should use ContactsContract instead.
Since you run it on Emulator API level 10, and the class is deprecated since API level 5, there's no reason to keep using People.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文