我试图在我的应用中获取联系人。它适用于普通的联系人但我的应用程序崩溃时,我尝试选择与Facebook同步的电话簿中的联系人(即Facebook联系人)。请帮忙。提前谢谢你。
这几乎是我处理电话簿的代码
public void onClickSelectContact(View btnSelectContact) {
// using native contacts selection
// Intent.ACTION_PICK = Pick an item from the data, returning what was selected.
startActivityForResult(new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI), REQUEST_CODE_PICK_CONTACTS);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_PICK_CONTACTS && resultCode == RESULT_OK) {
Log.d(TAG, "Response: " + data.toString());
uriContact = data.getData();
retrieveContactName();
retrieveContactNumber();
retrieveContactPhoto();
}
}
private void retrieveContactPhoto() {
Bitmap photo = null;
try {
InputStream inputStream = ContactsContract.Contacts.openContactPhotoInputStream(getContentResolver(),
ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, new Long(contactID)));
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream);
ImageView imageView = (ImageView) findViewById(R.id.img_contact);
imageView.setImageBitmap(photo);
}
assert inputStream != null;
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private void retrieveContactNumber() {
// getting contacts ID
Cursor cursorID = getContentResolver().query(uriContact,
new String[]{ContactsContract.Contacts._ID},
null, null, null);
if (cursorID.moveToFirst()) {
contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
}
cursorID.close();
Log.d(TAG, "Contact ID: " + contactID);
// Using the contact ID now we will get contact phone number
Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
new String[]{contactID},
null);
if (cursorPhone.moveToFirst()) {
contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
cursorPhone.close();
Log.d(TAG, "Contact Phone Number: " + contactNumber);
}
private void retrieveContactName() {
loanedTo = null;
// querying contact data store
Cursor cursor = getContentResolver().query(uriContact, null, null, null, null);
if (cursor.moveToFirst()) {
// DISPLAY_NAME = The display name for the contact.
// HAS_PHONE_NUMBER = An indicator of whether this contact has at least one phone number.
loanedTo = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
personName.setText(loanedTo);
}
cursor.close();
Log.d(TAG, "Contact Name: " + loanedTo);
}
logcat显示
02-05 18:32:34.382: E/AndroidRuntime(16628): FATAL EXCEPTION: main
02-05 18:32:34.382: E/AndroidRuntime(16628): FATAL EXCEPTION: main
02-05 18:32:34.382: E/AndroidRuntime(16628): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent { dat=content://com.android.contacts/contacts/lookup/1670i1213151574/3812 }} to activity {appytite.giveitback/appytite.giveitback.AddingItem}: java.lang.IllegalArgumentException: the bind value at index 1 is null
02-05 18:32:34.382: E/AndroidRuntime(16628): at android.app.ActivityThread.deliverResults(ActivityThread.java:3145)
02-05 18:32:34.382: E/AndroidRuntime(16628): at android.app.ActivityThread.handleSendResult(ActivityThread.java:3188)
02-05 18:32:34.382: E/AndroidRuntime(16628): at android.app.ActivityThread.access$1100(ActivityThread.java:134)
02-05 18:32:34.382: E/AndroidRuntime(16628): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
02-05 18:32:34.382: E/AndroidRuntime(16628): at android.os.Handler.dispatchMessage(Handler.java:99)
02-05 18:32:34.382: E/AndroidRuntime(16628): at android.os.Looper.loop(Looper.java:137)
02-05 18:32:34.382: E/AndroidRuntime(16628): at android.app.ActivityThread.main(ActivityThread.java:4744)
02-05 18:32:34.382: E/AndroidRuntime(16628): at java.lang.reflect.Method.invokeNative(Native Method)
02-05 18:32:34.382: E/AndroidRuntime(16628): at java.lang.reflect.Method.invoke(Method.java:511)
02-05 18:32:34.382: E/AndroidRuntime(16628): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-05 18:32:34.382: E/AndroidRuntime(16628): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-05 18:32:34.382: E/AndroidRuntime(16628): at dalvik.system.NativeStart.main(Native Method)
02-05 18:32:34.382: E/AndroidRuntime(16628): Caused by: java.lang.IllegalArgumentException: the bind value at index 1 is null
02-05 18:32:34.382: E/AndroidRuntime(16628): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:170)
02-05 18:32:34.382: E/AndroidRuntime(16628): at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:140)
02-05 18:32:34.382: E/AndroidRuntime(16628): at android.content.ContentProviderProxy.query(ContentProviderNative.java:366)
02-05 18:32:34.382: E/AndroidRuntime(16628): at android.content.ContentResolver.query(ContentResolver.java:370)
02-05 18:32:34.382: E/AndroidRuntime(16628): at android.content.ContentResolver.query(ContentResolver.java:313)
02-05 18:32:34.382: E/AndroidRuntime(16628): at appytite.giveitback.AddingItem.retrieveContactNumber(AddingItem.java:372)
02-05 18:32:34.382: E/AndroidRuntime(16628): at appytite.giveitback.AddingItem.onActivityResult(AddingItem.java:324)
02-05 18:32:34.382: E/AndroidRuntime(16628): at android.app.Activity.dispatchActivityResult(Activity.java:5192)
02-05 18:32:34.382: E/AndroidRuntime(16628): at android.app.ActivityThread.deliverResults(ActivityThread.java:3141)
02-05 18:32:34.382: E/AndroidRuntime(16628): ... 11 more