2011-12-14 170 views
0

我正在一个项目,我需要列出所有的联系人。我正在关注this articleAndroid java.lang.IllegalArgumentException:无效的列_count错误,同时检索联系人

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 

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

回答

1

Android People类已弃用。您应该使用ContactsContract。 由于您在仿真器API级别10上运行它,并且该类从API级别5开始被弃用,所以没有理由继续使用People。