浅水咸鱼
2016-03-06 05:29
采纳率: 72.7%
浏览 1.4k

android如何获取联系人列表,我下面这样获取错在哪

我写了一个安卓小程序,需要获取联系人姓名·和号码,下面的代码是我查资料得来的,可是运行不成功,错在哪
private Thread t1 ;
private ReadNameAndNum rnan;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
callView=View.inflate(this, R.layout.call, null);
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();

    这里我开了一个线程获取,因为之前用main线程获取屏幕卡死,但开这个线程后仍   是卡死,不知原因

下面是报错的日志内容,它说Permission Denial,但我的权限已经加了[code=java]03-06 13: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.permission.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.297: E/Parcel(19335): Reading a NULL string not supported here.
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.
03-06 13:20:57.637: I/System.out(19335): >>>>>onStop
03-06 13:20:57.637: I/System.out(19335): >>>>>ondestroy
03-06 13:20:57.637: I/System.out(19335): >>>>>CAllonStop
03-06 13:20:57.637: I/System.out(19335): >>>>>CAllondestroy
[/code]

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • 浅水咸鱼 2016-03-06 05:32
    已采纳

    不好意思,代码贴得好乱

    点赞 打赏 评论
  • 浅水咸鱼 2016-03-06 05:34

    我重新发布一个图片说明

    点赞 打赏 评论
  • 东风玖哥 2016-03-07 03:15

    你采纳了自己的自问自答?

    点赞 打赏 评论

相关推荐 更多相似问题