我正在android中实现contactPicker
我创建了一个图像按钮并单击该按钮我开始 activityforresult 以获取编辑文本中的单个联系人电话号码
但问题是 data 始终为 null 。
这是我的代码
public static final int PICK_CONTACT = 1;
ImageButton contactsButton = (ImageButton) findViewById(R.id.contacts_button);
contactsButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent,PICK_CONTACT);
}
});
这是活动结果
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
Log.d(" data", data+""); //returning null
Log.d("resultCode", resultCode+""); //returning 0
Log.d("reqCode", reqCode+""); //returning 1
Log.d("Activity2.RESULT_OK", Activity2.RESULT_OK+"");//returning -1
if (resultCode == Activity2.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String contactId = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
String hasNumber = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
String num = "";
if (Integer.valueOf(hasNumber) == 1) {
Cursor numbers = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + contactId, null, null);
while (numbers.moveToNext()) {
num = numbers.getString(numbers.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(xxx.this, "Number="+num, Toast.LENGTH_LONG).show();
ed= (EditText)findViewById(R.id.txtoooo);
ed.setText(num);
}
}
}
break;
}
}
}
我要做的就是选择一个联系人并将其电话号码加载到 EditText 。请帮助我,我尝试了互联网上所有可用的解决方案..我在哪里犯了错误?