我使用以下代码从我的应用程序添加自定义联系人:
public static void addContact(Searchable contact,String name,String ContactKey ,final Activity ctx){
initializeDB(ctx);
if(Groupid== null)
Groupid = getGroupId(ctx);
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, name)
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.phone)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,Phone.TYPE_WORK)
.build());
if(contact.phone2 !=" "){
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.phone2)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,Phone.TYPE_WORK)
.build());
}
if(contact.phone3 !=" "){
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.phone3)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,Phone.TYPE_WORK)
.build());
}
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.fax)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,Phone.TYPE_FAX_WORK)
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.StructuredPostal.STREET,(" ".equals(contact.getAddress())?contact.location:contact.getAddress()))
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Organization.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Organization.COMPANY,ctx.getResources().getString(R.string.meuhedet))
.build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.GroupMembership.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.GroupMembership.GROUP_ROW_ID, Long.parseLong(Groupid))
.build());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap photo = BitmapFactory.decodeResource(ctx.getResources(),R.drawable.logo_meuhedet);
photo.compress(Bitmap.CompressFormat.PNG, 100, baos);
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Photo.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Photo.PHOTO, baos.toByteArray())
.build());
try {
ContentProviderResult [] res =ctx.getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
contactID = ContentUris.parseId(res[0].uri);
boolean result = db.saveContactsIntoDatabase(contact.index, ContactKey, contactID);
if(result){
((Activity) ctx).runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(ctx, R.string.successfully_saved_contact, Toast.LENGTH_SHORT).show();
}
});
}
} catch (Exception e) {
}
}
联系人保存在联系人列表中。 当我尝试genter dialer应用程序崩溃时出现此异常:
12-23 11:17:55.172: E/AndroidRuntime(8233): FATAL EXCEPTION: AsyncTask #4
12-23 11:17:55.172: E/AndroidRuntime(8233): Process: com.google.android.dialer, PID: 8233
12-23 11:17:55.172: E/AndroidRuntime(8233): java.lang.RuntimeException: An error occured while executing doInBackground()
12-23 11:17:55.172: E/AndroidRuntime(8233): at android.os.AsyncTask$3.done(AsyncTask.java:300)
12-23 11:17:55.172: E/AndroidRuntime(8233): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
12-23 11:17:55.172: E/AndroidRuntime(8233): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
12-23 11:17:55.172: E/AndroidRuntime(8233): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
12-23 11:17:55.172: E/AndroidRuntime(8233): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-23 11:17:55.172: E/AndroidRuntime(8233): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-23 11:17:55.172: E/AndroidRuntime(8233): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-23 11:17:55.172: E/AndroidRuntime(8233): at java.lang.Thread.run(Thread.java:841)
12-23 11:17:55.172: E/AndroidRuntime(8233): Caused by: java.lang.IllegalArgumentException: the bind value at index 2 is null
12-23 11:17:55.172: E/AndroidRuntime(8233): at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:164)
12-23 11:17:55.172: E/AndroidRuntime(8233): at com.android.dialer.database.DialerDatabaseHelper.insertUpdatedContactsAndNumberPrefix(DialerDatabaseHelper.java:632)
12-23 11:17:55.172: E/AndroidRuntime(8233): at com.android.dialer.database.DialerDatabaseHelper.updateSmartDialDatabase(DialerDatabaseHelper.java:784)
12-23 11:17:55.172: E/AndroidRuntime(8233): at com.android.dialer.database.DialerDatabaseHelper$SmartDialUpdateAsyncTask.doInBackground(DialerDatabaseHelper.java:481)
12-23 11:17:55.172: E/AndroidRuntime(8233): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-23 11:17:55.172: E/AndroidRuntime(8233): ... 4 more
当我打开source code并查看应用程序崩溃的行时,它在此行上失败:
insert.bindString(2, updatedContactCursor.getString(PhoneQuery.PHONE_NUMBER));
有人出现这个问题吗?有什么问题(我添加这个字符串!!)?
答案 0 :(得分:0)
我发现了问题。
连接到需要“ContactsContract.CommonDataKinds.Phone.NUMBER”的所有值都必须不为null,因为当至少一个值为null时代码崩溃。
这解决了我的异常:
.....
//need to check because if is null it crash dialer in SDK 4.4.2
if(contact.phone != null && contact.phone !=" "){
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.phone)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,Phone.TYPE_WORK)
.build());
}
if(contact.phone2 != null && contact.phone2 !=" "){
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.phone2)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,Phone.TYPE_WORK)
.build());
}
if(contact.phone3 != null && contact.phone3 !=" "){
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.phone3)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,Phone.TYPE_WORK)
.build());
}
if(contact.fax != null && contact.fax !=" "){
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0)
.withValue(ContactsContract.Data.MIMETYPE,ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, contact.fax)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE,Phone.TYPE_FAX_WORK)
.build());
}
.....