我在 Android 中编辑联系人时遇到问题。我正在使用contentProviderOperation
with newUpdate
,但它不起作用。它仅在该字段已有内容时才有效。如果我填写了一个字段,请删除它,然后保存并再次尝试编辑它,我发现它没有保存,这就是我得到异常的时候。
这是我必须编辑的代码:
if (emailantigo.equals("") && !email.equals("")) {
debug("email " + email);
debug("id " + contactId);
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.RawContacts.CONTACT_ID, contactId)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS,
email).build());
} else if (!email.equals(emailantigo)) {
debug("email " + email);
debug("id " + contactId);
ops.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(
ContactsContract.RawContacts.CONTACT_ID
+ "=? AND "
+ ContactsContract.Data.MIMETYPE
+ "='"
+ ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE
+ "'",
new String[] { String.valueOf(contactId) })
.withValue(ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS,
email).build());
}
try {
cr.applyBatch(ContactsContract.AUTHORITY, ops);
} catch (Exception e) {
e.printStackTrace();
}
public void debug(String msg) {
Log.i("CONTATOS", msg);
}
这是我从 LogCat 得到的异常:
java.lang.NullPointerException
android.os.Parcel.readException(Parcel.java:1266)
android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:160)
android.database.DatabaseUtils.readExceptionWithOperationApplicationExceptionFromParcel(DatabaseUtils.java:137)
android.content.ContentProviderProxy.applyBatch(ContentProviderNative.java:460)
android.content.ContentProviderClient.applyBatch(ContentProviderClient.java:95)
android.content.ContentResolver.applyBatch(ContentResolver.java:622)
** 编辑 2 **
自己找到解决方案
有效的部分代码:
Cursor cursorEmail = cr.query(
ContactsContract.CommonDataKinds.Email.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = "
+ contactId, null, null);
if (cursorEmail.moveToFirst()) {
if (!email.equals(emailantigo))
key_email = ops
.add(ContentProviderOperation
.newUpdate(ContactsContract.Data.CONTENT_URI)
.withSelection(
ContactsContract.Data.CONTACT_ID
+ "=? AND "
+ ContactsContract.Data.MIMETYPE
+ "=? AND "
+ ContactsContract.CommonDataKinds.Email.TYPE
+ "=?",
new String[] {
String.valueOf(contactId),
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE,
String.valueOf(ContactsContract.CommonDataKinds.Email.TYPE_WORK) })
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(
ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.withValue(
ContactsContract.CommonDataKinds.Email.ADDRESS,
email).build());
}
cursorEmail.close();
if (!key_email && emailantigo.equals("") && !email.equals(emailantigo)) {
ops.add(ContentProviderOperation
.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.RAW_CONTACT_ID,
String.valueOf(contactId))
.withValue(
ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Email.ADDRESS,
email)
.withValue(ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_WORK)
.build());
}
newInsert 不适用于 Data._ID 或 Data.Contact_ID,我不知道为什么,这背后一定有一些逻辑,但它现在可以工作了^^!
Obs:未在同步联系人中测试。