【问题标题】:Android java.lang.IllegalArgumentException: Invalid column _count error while retrieving contactsAndroid java.lang.IllegalArgumentException:检索联系人时出现无效列_count错误
【发布时间】:2011-12-15 01:49:11
【问题描述】:

我正在做一个需要列出所有联系人的项目。我正在关注this article

我的 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

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

【问题讨论】:

    标签: android android-contacts illegalargumentexception


    【解决方案1】:

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

    另请参阅:Android SDK - List All Users

    【讨论】:

      【解决方案2】:

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

      【讨论】:

        猜你喜欢
        • 2015-04-17
        • 1970-01-01
        • 2011-05-25
        • 1970-01-01
        • 2011-09-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-01-04
        相关资源
        最近更新 更多