在 HTC 上插入联系人

发布于 2024-11-24 21:11:40 字数 3774 浏览 1 评论 0原文

我在 HTC 设备中插入联系人时遇到问题, 看起来它在其他设备上运行良好。 我使用的代码来自:在 Android 2.2 中插入联系人

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();

ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
                    .withValue(RawContacts.ACCOUNT_TYPE, null)
                    .withValue(RawContacts.ACCOUNT_NAME,null )
                    .build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
                    .withValue(Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE)
                    .withValue(Phone.NUMBER, "9X-XXXXXXXXX")
                    .build());
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                    .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
                    .withValue(Data.MIMETYPE,
                     StructuredName.CONTENT_ITEM_TYPE)
                    .withValue(StructuredName.DISPLAY_NAME, "Mike Sullivan")
                    .build());  
ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

,我得到:

07-18 09:37:49.832: ERROR/DatabaseUtils(209): Writing exception to parcel
07-18 09:37:49.832: ERROR/DatabaseUtils(209): java.lang.NullPointerException
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at    com.android.providers.contacts.HtcContactsProvider2$PhoneDataRowHandler.insert(HtcContactsPr ovider2.java:4614)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at com.android.providers.contacts.ContactsProvider2.insertData(ContactsProvider2.java:3599)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at com.android.providers.contacts.ContactsProvider2.insertInTransaction(ContactsProvider2.java:3394)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at com.android.providers.contacts.CContactsProvider2.insertInTransaction(CContactsProvider2.java:191)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at com.android.providers.contacts.HtcContactsProvider2.insertInTransaction(HtcContactsProvider2.java:1677)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at com.android.providers.contacts.SQLiteContentProvider.insert(SQLiteContentProvider.java:106)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at com.android.providers.contacts.ContactsProvider2.insert(ContactsProvider2.java:3159)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at com.android.providers.contacts.CContactsProvider2.insert(CContactsProvider2.java:117)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at android.content.ContentProviderOperation.apply(ContentProviderOperation.java:214)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at com.android.providers.contacts.SQLiteContentProvider.applyBatch(SQLiteContentProvider.java:216)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at com.android.providers.contacts.ContactsProvider2.applyBatch(ContactsProvider2.java:3193)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at com.android.providers.contacts.CContactsProvider2.applyBatch(CContactsProvider2.java:145)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at android.content.ContentProvider$Transport.applyBatch(ContentProvider.java:199)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:202)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at android.os.Binder.execTransact(Binder.java:288)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at dalvik.system.NativeStart.run(Native Method)

I have problems with inserting contacts in HTC devices,
it looks that it works fine on other devices.
I'm using code from: Inserting contacts in Android 2.2

ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
int rawContactInsertIndex = ops.size();

ops.add(ContentProviderOperation.newInsert(RawContacts.CONTENT_URI)
                    .withValue(RawContacts.ACCOUNT_TYPE, null)
                    .withValue(RawContacts.ACCOUNT_NAME,null )
                    .build());
ops.add(ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI)
                    .withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, rawContactInsertIndex)
                    .withValue(Data.MIMETYPE,Phone.CONTENT_ITEM_TYPE)
                    .withValue(Phone.NUMBER, "9X-XXXXXXXXX")
                    .build());
ops.add(ContentProviderOperation.newInsert(Data.CONTENT_URI)
                    .withValueBackReference(Data.RAW_CONTACT_ID, rawContactInsertIndex)
                    .withValue(Data.MIMETYPE,
                     StructuredName.CONTENT_ITEM_TYPE)
                    .withValue(StructuredName.DISPLAY_NAME, "Mike Sullivan")
                    .build());  
ContentProviderResult[] res = getContentResolver().applyBatch(ContactsContract.AUTHORITY, ops);

and I'm getting:

07-18 09:37:49.832: ERROR/DatabaseUtils(209): Writing exception to parcel
07-18 09:37:49.832: ERROR/DatabaseUtils(209): java.lang.NullPointerException
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at    com.android.providers.contacts.HtcContactsProvider2$PhoneDataRowHandler.insert(HtcContactsPr ovider2.java:4614)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at com.android.providers.contacts.ContactsProvider2.insertData(ContactsProvider2.java:3599)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at com.android.providers.contacts.ContactsProvider2.insertInTransaction(ContactsProvider2.java:3394)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at com.android.providers.contacts.CContactsProvider2.insertInTransaction(CContactsProvider2.java:191)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at com.android.providers.contacts.HtcContactsProvider2.insertInTransaction(HtcContactsProvider2.java:1677)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at com.android.providers.contacts.SQLiteContentProvider.insert(SQLiteContentProvider.java:106)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at com.android.providers.contacts.ContactsProvider2.insert(ContactsProvider2.java:3159)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at com.android.providers.contacts.CContactsProvider2.insert(CContactsProvider2.java:117)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at android.content.ContentProviderOperation.apply(ContentProviderOperation.java:214)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at com.android.providers.contacts.SQLiteContentProvider.applyBatch(SQLiteContentProvider.java:216)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at com.android.providers.contacts.ContactsProvider2.applyBatch(ContactsProvider2.java:3193)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at com.android.providers.contacts.CContactsProvider2.applyBatch(CContactsProvider2.java:145)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at android.content.ContentProvider$Transport.applyBatch(ContentProvider.java:199)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:202)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at android.os.Binder.execTransact(Binder.java:288)
07-18 09:37:49.832: ERROR/DatabaseUtils(209):     at dalvik.system.NativeStart.run(Native Method)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

枉心 2024-12-01 21:11:40

这个答案:
https://stackoverflow.com/a/4387890/244521

建议将 Phone.TYPE 添加到您的 ContactsContract.Data.CONTENT_URI 插入中。 Android 文档有一个很好的示例:
http://developer.android.com/reference/android/provider/ ContactsContract.Data.html

ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "1-800-GOOG-411");
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, "free directory assistance");
Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);

您发布的代码没有 Phone.TYPE 行。

This answer:
https://stackoverflow.com/a/4387890/244521

Suggests adding Phone.TYPE to your ContactsContract.Data.CONTENT_URI insert. The Android documentation has a good example:
http://developer.android.com/reference/android/provider/ContactsContract.Data.html

ContentValues values = new ContentValues();
values.put(Data.RAW_CONTACT_ID, rawContactId);
values.put(Data.MIMETYPE, Phone.CONTENT_ITEM_TYPE);
values.put(Phone.NUMBER, "1-800-GOOG-411");
values.put(Phone.TYPE, Phone.TYPE_CUSTOM);
values.put(Phone.LABEL, "free directory assistance");
Uri dataUri = getContentResolver().insert(Data.CONTENT_URI, values);

The code you posted has no line for Phone.TYPE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文