在HTC上插入联系人

时间:2011-07-18 08:10:16

标签: android contacts htc-android

我在HTC设备中插入联系人时遇到问题, 看起来它在其他设备上运行良好。 我使用的代码来自: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);

我得到了:

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)

1 个答案:

答案 0 :(得分:2)

这个回答:
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的行。