getContentResolver为ContactsContract.Contacts返回null

时间:2016-06-09 16:41:31

标签: android contact android-contentresolver contactscontract

从主要活动调用活动,活动通过意图调用CONTENT_URI

 Intent intent=new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
    startActivityForResult(intent, PICK_CONTACT);

OnActivityResult我试图获取id,联系人姓名和电话但查询光标返回null

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    ContentResolver cr = getContentResolver();
    String cNumber="";
    switch (requestCode) {
        case (PICK_CONTACT) :
            if (resultCode == Activity.RESULT_OK) {
                Uri contactData = data.getData();
                Cursor c =  cr.query(contactData, null, null, null, null);
                if (c.moveToFirst()) {
                    String id =c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                    String hasPhone =c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                    if (hasPhone.equalsIgnoreCase("1")) {
                        Cursor phones =cr.query(
                                ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                                ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id,
                                null, null);
                        phones.moveToFirst();
                        cNumber = phones.getString(phones.getColumnIndex("data1"));
                    }
                    String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

                    Log.i("name",name);
                    Log.i("nameno", cNumber);
                   // arrayList.clear();
                    for(int i=0;i<arrayList.size(); i++)
                    {
                        String fromlist=arrayList.get(i).toString();
                        String addedtolist=name +"-" + cNumber;
                        if(fromlist.equals(addedtolist))
                        {
                            Toast.makeText(this, "Conatct already exists. ", Toast.LENGTH_LONG).show();
                            isExists=1;
                        }
                    }
                    if(isExists==0)
                    {
                        arrayList.add(name + "-" + cNumber);
                        lstContacts.setAdapter(arrayAdapter);
                        arrayAdapter.notifyDataSetChanged();
                        writeToFile(name + "-" + cNumber, "_ShareConatcts.txt");
                       sharecontact(android_id,name,cNumber,1);
                    }
                    isExists=0;
                }
            }
            break;
    }
}

上面的命令工作了几天,但突然开始给出null,它甚至没有抛出任何错误。调试游标C时返回null。

0 个答案:

没有答案