2 qq 30130435 qq_30130435 于 2016.03.06 13:29 提问

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个回答

qq_30130435
qq_30130435   2016.03.06 13:32
已采纳

不好意思,代码贴得好乱

qq_30130435
qq_30130435   2016.03.06 13:34

我重新发布一个图片说明

dongfeng9ge
dongfeng9ge   Ds   Rxr 2016.03.07 11:15

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

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!