我创建了一个onActivityResult方法。我在联系人列表中,点击后我会得到联系人的姓名和电话号码,但它崩溃了。
@Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
//TODO Auto-generated method stub
super.onActivityResult(reqCode, resultCode, data);
if (reqCode == PICK_CONTACT) {
if(resultCode == ActionBarActivity.RESULT_OK){
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if(c.moveToFirst()) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String phone = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(this, "You've picked" + name, Toast.LENGTH_SHORT).show();
}
}
}
}
请注意,如果我删除该行
String phone = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
它没有崩溃。 (但我没有收到电话号码。