如何在 Android 中以编程方式向联系人添加电话号码?
我有一个可以很好地添加和删除联系人的应用程序。如果现有联系人中有现有值,则可以对其进行修改。但我似乎无法将新值插入现有联系人中。例如,如果家庭电话号码有现有值,但工作电话号码没有,我尝试使用填充来添加值(cintact idValue
和 workNumber
被传入):
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.CONTACT_ID, idValue)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, workNumber)
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
但是我得到一个 NullPointerException:
java.lang.NullPointerException
at com.android.providers.contacts.ContactsProvider2.insertData(ContactsProvider2.java:2604)
at com.android.providers.contacts.ContactsProvider2.insertInTransaction(ContactsProvider2.java:2452)
at com.android.providers.contacts.SQLiteContentProvider.insert(SQLiteContentProvider.java:106)
at com.android.providers.contacts.ContactsProvider2.insert(ContactsProvider2.java:2256)
at android.content.ContentProviderOperation.apply(ContentProviderOperation.java:214)
at com.android.providers.contacts.SQLiteContentProvider.applyBatch(SQLiteContentProvider.java:216)
at com.android.providers.contacts.ContactsProvider2.applyBatch(ContactsProvider2.java:2290)
at android.content.ContentProvider$Transport.applyBatch(ContentProvider.java:217)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:194)
at android.os.Binder.execTransact(Binder.java:336)
at dalvik.system.NativeStart.run(Native Method)
有人可以告诉我我做错了什么吗?
I have an app that can add and delete contacts just fine. And if there is an existing value in an existing contact, it can be modified just fine. But I can't seem to be able to insert new values into existing contacts. For example, if there is an existing value for a home phone number, but not for the work phone number, I tried using the fillowing to add the value (cintact idValue
and workNumber
are passed in):
ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
.withValue(ContactsContract.Data.CONTACT_ID, idValue)
.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE)
.withValue(ContactsContract.CommonDataKinds.Phone.TYPE, ContactsContract.CommonDataKinds.Phone.TYPE_WORK)
.withValue(ContactsContract.CommonDataKinds.Phone.NUMBER, workNumber)
.build());
getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);
But I get a NullPointerException:
java.lang.NullPointerException
at com.android.providers.contacts.ContactsProvider2.insertData(ContactsProvider2.java:2604)
at com.android.providers.contacts.ContactsProvider2.insertInTransaction(ContactsProvider2.java:2452)
at com.android.providers.contacts.SQLiteContentProvider.insert(SQLiteContentProvider.java:106)
at com.android.providers.contacts.ContactsProvider2.insert(ContactsProvider2.java:2256)
at android.content.ContentProviderOperation.apply(ContentProviderOperation.java:214)
at com.android.providers.contacts.SQLiteContentProvider.applyBatch(SQLiteContentProvider.java:216)
at com.android.providers.contacts.ContactsProvider2.applyBatch(ContactsProvider2.java:2290)
at android.content.ContentProvider$Transport.applyBatch(ContentProvider.java:217)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:194)
at android.os.Binder.execTransact(Binder.java:336)
at dalvik.system.NativeStart.run(Native Method)
Can someone please tell me what I'm doing wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您缺少
withValue(Phone.TYPE, Phone.TYPE_WORK)
编辑
您是正确的,我检查了
data
表的架构,它是遵循它仅使用原始联系人 ID,而不使用联系人 IDYou are missing
withValue(Phone.TYPE, Phone.TYPE_WORK)
Edits
You are correct I checked the schema for the
data
table and it is as follow it is only using raw contacts id and not contact id您收到错误是因为 Android 不允许您直接将联系人添加到您的联系人表中。相反,您需要修改或添加原始联系人,Android 会自动为您创建联系人。
You were getting error because Android doesn't give you access to add a contact directly to you Contact table. Rather you need to modify or add a raw Contact and Android will automatically create a Contact for you.