2 qq 30130435 qq_30130435 于 2016.03.06 13:49 提问

android如何获取电话联系人列表,我这样写错在哪
private ReadNameAndNum rnan;
private Thread t1 ;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    callView=View.inflate(this, R.layout.call, null);
    callViewLayer=new LockUtil(this);
    callViewLayer.setLockView(callView);
    callViewLayer.lock();
    callView.findViewById(R.id.callBack).setOnClickListener(this);

    rnan=new ReadNameAndNum();
    t1=new Thread(rnan);
    t1.start();


}
class ReadNameAndNum implements Runnable{
    ContentResolver cr= getContentResolver();
    Cursor c=cr.query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null);
    @Override
    public void run() {
        System.out.println(">>>>>RUN");
        while (c.moveToNext()) {
            int nameFieldColumnIndex= c.getColumnIndex(PhoneLookup.DISPLAY_NAME);
            String contact=c.getString(nameFieldColumnIndex);

            int numberFieldColumnIndex=c.getColumnIndex(PhoneLookup.NUMBER);
            String number=c.getString(numberFieldColumnIndex);

            System.out.println(">>>>>"+contact+">>>>"+number);
        }
        c.close();
    }}

```报错日志
     20:35.177: I/System.out(19335): >>>>>onStart
     03-06 13:20:35.177: I/System.out(19335): >>>>>onResume
      03-06 13:20:37.027: I/System.out(19335): >>>>CallClick
     03-06 13:20:57.297: W/ServiceManager(19335): Permission failure: android.p   ermission.READ_CONTACTS from uid=10104 pid=19335
     03-06 13:20:57.297: E/CheckPermission(19335): Permission Denial: can't read contacts pid=19335, uid=10104
     03-06 13:20:57.307: I/System.out(19335): >>>>>CAllonStart
     03-06 13:20:57.307: I/System.out(19335): >>>>>CAllonResume
     03-06 13:20:57.317: I/System.out(19335): >>>>>RUN
    03-06 13:20:57.317: W/dalvikvm(19335): threadid=16: thread exiting with uncaught exception (group=0x41697898)
     03-06 13:20:57.327: E/AndroidRuntime(19335): FATAL EXCEPTION: Thread-1466
     03-06 13:20:57.327: E/AndroidRuntime(19335): java.lang.NullPointerException
     03-06 13:20:57.327: E/AndroidRuntime(19335):   at com.example.deng_00_lock.AtyCall$ReadNameAndNum.run(AtyCall.java:42)
     03-06 13:20:57.327: E/AndroidRuntime(19335):   at java.lang.Thread.run(Thread.java:841)
     03-06 13:20:57.397: I/Choreographer(19335): Skipped 1207 frames!  The application may be doing too much work on its main thread.


读取权限我也加了,但仍不成功,这里我开了一个线程


7个回答

qq519934599
qq519934599   2016.03.10 09:01
已采纳

private static String uri_rawcontacts = "content://com.android.contacts/raw_contacts";
private static String uri_contacts_phones = "content://com.android.contacts/data/phones";
private static String uri_contacts_emails = "content://com.android.contacts/data/emails";
private String uri_contacts_data = "content://com.android.contacts/data";

// 查询联系人的信息
public static List<Map<String, Object>> selectContactsInfo(
        ContentResolver resolver) {
    List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
    Cursor contactsCursor = resolver.query(Uri.parse(uri_rawcontacts),
            new String[] { "_id", "display_name" }, null, null, null);
    while (contactsCursor.moveToNext()) {
        Map<String, Object> map = new HashMap<String, Object>();
        int contactsId = contactsCursor.getInt(contactsCursor
                .getColumnIndex("_id"));
        String displayName = contactsCursor.getString(contactsCursor
                .getColumnIndex("display_name"));
        map.put("_id", contactsId);
        map.put("display_name", displayName);

        // 根据联系人的id去data表获取电话号码的信息
        Cursor phoneCursor = resolver.query(Uri.parse(uri_contacts_phones),
                new String[] { "raw_contact_id", "data1" },
                "raw_contact_id=?", new String[] { contactsId + "" }, null);

        StringBuilder sb = new StringBuilder();
        while (phoneCursor.moveToNext()) {
            sb.append(phoneCursor.getString(1));
            sb.append("|");
        }
        map.put("phones", sb.toString());
        if (phoneCursor != null) {
            phoneCursor.close();
        }

        // 根据联系人的id去data表获取email的信息
        Cursor emailCursor = resolver.query(Uri.parse(uri_contacts_emails),
                new String[] { "raw_contact_id", "data1" },
                "raw_contact_id=?", new String[] { contactsId + "" }, null);

        StringBuilder sb2 = new StringBuilder();
        while (emailCursor.moveToNext()) {
            sb2.append(emailCursor.getString(1));
            sb2.append("|");
        }
        map.put("emails", sb2.toString());
        if (emailCursor != null) {
            emailCursor.close();
        }

        list.add(map);
    }
    if (contactsCursor != null) {
        contactsCursor.close();
    }
    return list;
}
qq_30130435
qq_30130435 十分感谢,后几天我再试试
接近 2 年之前 回复
yale8848
yale8848   2016.03.06 15:46

检查一下是添加了READ_CONTACTS权限吗?添加的位置对吗?

qq_30130435
qq_30130435 填了,位置对了,user permission
接近 2 年之前 回复
qq_30130435
qq_30130435   2016.03.06 18:05

搞了个乌龙,原来是我的手机在程序请求权限的时候没授予,所以没得到列表,但现在仍有个问题,就是号码取不出来,名字到取出来了

crazy1235
crazy1235   Ds   Rxr 2016.03.06 22:14

Permission Denial

还是没权限的问题。

号码取不出来估计是你字段搞作了。

qq_30130435
qq_30130435 你好!我现在的错误是这样,我看不懂,请问错在哪
接近 2 年之前 回复
qq_30130435
qq_30130435 Failed to read row 0, column -1 from a CursorWindow which has 161 rows, 39 columns. 03-07 18:46:44.049: W/dalvikvm(14502): threadid=23: thread exiting with uncaught exception (group=0x41695898) 03-07 18:46:44.049: E/AndroidRuntime(14502): FATAL EXCEPTION: Thread-1841 03-07 18:46:44.049: E/AndroidRuntime(14502): java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
接近 2 年之前 回复
qq_30130435
qq_30130435 Failed to read row 0, column -1 from a CursorWindow which has 161 rows, 39 columns. 03-07 18:46:44.049: W/dalvikvm(14502): threadid=23: thread exiting with uncaught exception (group=0x41695898) 03-07 18:46:44.049: E/AndroidRuntime(14502): FATAL EXCEPTION: Thread-1841 03-07 18:46:44.049: E/AndroidRuntime(14502): java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
接近 2 年之前 回复
dongfeng9ge
dongfeng9ge   Ds   Rxr 2016.03.07 11:16

遇到任何和系统功能有关的功能的时候都要注意加权限

qq_30130435
qq_30130435 嗯嗯,已添加
接近 2 年之前 回复
qq519934599
qq519934599   2016.03.08 17:21

Couldn't read row 0, col -1 from CursorWindow。
不能从cursor视窗中读取第0行第-1列,
把nameFieldColumnIndex,numberFieldColumnIndex这两个int打印出来看看

qq519934599
qq519934599   2016.03.08 17:29

调查完了,这个cursor中貌似没有PhoneLookup.NUMBER这个字段,你把它去掉,就好了

qq519934599
qq519934599 回复qq_30130435: 看我下面这个回帖。
接近 2 年之前 回复
qq519934599
qq519934599 回复qq_30130435: 这里需要联合查询:
接近 2 年之前 回复
qq519934599
qq519934599 回复qq_30130435: 这里需要联合查询:
接近 2 年之前 回复
qq_30130435
qq_30130435 感谢帮忙!但去掉后如何取出号码
接近 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!