Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/216.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Can';我看不懂电话联系方式_Android_Contacts - Fatal编程技术网

Android Can';我看不懂电话联系方式

Android Can';我看不懂电话联系方式,android,contacts,Android,Contacts,我得到了这个例外 12-01 12:28:42.552: E/AndroidRuntime(25581): Caused by: java.lang.IllegalStateException: Couldn't read row 1, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 当我想使用以下代码读取手机(sim)联系人时 什么是

我得到了这个例外

12-01 12:28:42.552: E/AndroidRuntime(25581): Caused by: java.lang.IllegalStateException: Couldn't read row 1, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
当我想使用以下代码读取手机(sim)联系人时

什么是[问题和如何解决?

像这样做吗

if (cursor.moveToFirst()) {
    while (cursor.moveToNext()) { 
    String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
    String name= cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
    System.out.println(contactID + "   " + name);
    }
 }
它将在读取数据之前将光标移动到第一行

如果它对你有用,就把它当作正确的。:)

试试这个

 Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 
 if(cursor.moveToFirst()){
       while (cursor.moveToNext()) { 
       String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
       String name      = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

       System.out.println(contactID + "   " + name);
       }
 cursor.close();
 }
还要确保你在舱单上有这个

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

像这样试试

     Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null); 
    if(cursor != null && cursor.getCount() > 0){
if (cursor.moveToFirst()) {
    do {
        String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
            String name      = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 

          System.out.println(contactID + "   " + name);
    } while (cursor.moveToNext());

    }
        cursor.close();

用下面的代码替换你的第一行它对我有用

 Cursor cursor = 
        getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                     null, null, null, null);
您最好更改“if”循环中条件的顺序,以避免空指针异常(如果in-case游标为空)


如果答案对您有效,请接受。谢谢。

您在清单中有权限吗?
添加到您的清单中。xmlyes,我添加了它。请注意colmn-1Check
if(cur.getCount()>0)
我做了,但我做了相同的例外检查并做了这个词,cursor.moveToFirst()返回trueI,给出了相同的例外,但显示了联系人的姓名“未知”有什么问题吗?@ali:是显示所有姓名都未知还是只有少数姓名?这个问题与问题无关,这是另一回事,谢谢
 Cursor cursor = 
        getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                     null, null, null, null);
 if(cursor!=null && cursor.getCount()>0 && cursor.moveToFirst()){