CustomContact 插入时出现 NullPointerException

发布于 2024-12-06 06:37:01 字数 3927 浏览 3 评论 0原文

代码:

        ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();

        // Create raw contact
        Builder builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
        builder.withValue(RawContacts.ACCOUNT_NAME, "testaccount");
        builder.withValue(RawContacts.ACCOUNT_TYPE, "nl.my.project.account");
        builder.withValue(RawContacts.SYNC1, "username"); 
        operationList.add(builder.build());

        builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
        builder.withValueBackReference(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, 0);
        builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
        builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "name"); 
        operationList.add(builder.build());

        builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
        builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
        builder.withValue(ContactsContract.Data.MIMETYPE, "vnd.android.cursor.item/vnd.nl.my.project.profile");
        builder.withValue(ContactsContract.Data.DATA1, "username");
        builder.withValue(ContactsContract.Data.DATA2, "Profile");
        builder.withValue(ContactsContract.Data.DATA3, "View profile");
        operationList.add(builder.build());

        try {
            getContentResolver().applyBatch(ContactsContract.AUTHORITY, operationList);
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (OperationApplicationException e) {
            e.printStackTrace();
        }

contacts.xml:

<ContactsSource xmlns:android="http://schemas.android.com/apk/res/android">
    <ContactsDataKind
        android:icon="@drawable/icon"
        android:mimeType="vnd.android.cursor.item/vnd.nl.my.project.profile"
        android:summaryColumn="data2"
        android:detailColumn="data3"
        android:detailSocialSummary="true" />
</ContactsSource>

Logcat:

09-23 09:09:23.981: ERROR/DatabaseUtils(223): Writing exception to parcel
09-23 09:09:23.981: ERROR/DatabaseUtils(223): java.lang.NullPointerException
09-23 09:09:23.981: ERROR/DatabaseUtils(223):     at com.android.providers.contacts.ContactsProvider2.insertData(ContactsProvider2.java:2574)
09-23 09:09:23.981: ERROR/DatabaseUtils(223):     at com.android.providers.contacts.ContactsProvider2.insertInTransaction(ContactsProvider2.java:2422)
09-23 09:09:23.981: ERROR/DatabaseUtils(223):     at com.android.providers.contacts.SQLiteContentProvider.insert(SQLiteContentProvider.java:106)
09-23 09:09:23.981: ERROR/DatabaseUtils(223):     at com.android.providers.contacts.ContactsProvider2.insert(ContactsProvider2.java:2238)
09-23 09:09:23.981: ERROR/DatabaseUtils(223):     at android.content.ContentProviderOperation.apply(ContentProviderOperation.java:214)
09-23 09:09:23.981: ERROR/DatabaseUtils(223):     at com.android.providers.contacts.SQLiteContentProvider.applyBatch(SQLiteContentProvider.java:216)
09-23 09:09:23.981: ERROR/DatabaseUtils(223):     at com.android.providers.contacts.ContactsProvider2.applyBatch(ContactsProvider2.java:2272)
09-23 09:09:23.981: ERROR/DatabaseUtils(223):     at android.content.ContentProvider$Transport.applyBatch(ContentProvider.java:193)
09-23 09:09:23.981: ERROR/DatabaseUtils(223):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:173)
09-23 09:09:23.981: ERROR/DatabaseUtils(223):     at android.os.Binder.execTransact(Binder.java:288)
09-23 09:09:23.981: ERROR/DatabaseUtils(223):     at dalvik.system.NativeStart.run(Native Method)

The code:

        ArrayList<ContentProviderOperation> operationList = new ArrayList<ContentProviderOperation>();

        // Create raw contact
        Builder builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
        builder.withValue(RawContacts.ACCOUNT_NAME, "testaccount");
        builder.withValue(RawContacts.ACCOUNT_TYPE, "nl.my.project.account");
        builder.withValue(RawContacts.SYNC1, "username"); 
        operationList.add(builder.build());

        builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
        builder.withValueBackReference(ContactsContract.CommonDataKinds.StructuredName.RAW_CONTACT_ID, 0);
        builder.withValue(ContactsContract.Data.MIMETYPE, ContactsContract.CommonDataKinds.StructuredName.CONTENT_ITEM_TYPE);
        builder.withValue(ContactsContract.CommonDataKinds.StructuredName.DISPLAY_NAME, "name"); 
        operationList.add(builder.build());

        builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);
        builder.withValueBackReference(ContactsContract.Data.RAW_CONTACT_ID, 0);
        builder.withValue(ContactsContract.Data.MIMETYPE, "vnd.android.cursor.item/vnd.nl.my.project.profile");
        builder.withValue(ContactsContract.Data.DATA1, "username");
        builder.withValue(ContactsContract.Data.DATA2, "Profile");
        builder.withValue(ContactsContract.Data.DATA3, "View profile");
        operationList.add(builder.build());

        try {
            getContentResolver().applyBatch(ContactsContract.AUTHORITY, operationList);
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (OperationApplicationException e) {
            e.printStackTrace();
        }

contacts.xml:

<ContactsSource xmlns:android="http://schemas.android.com/apk/res/android">
    <ContactsDataKind
        android:icon="@drawable/icon"
        android:mimeType="vnd.android.cursor.item/vnd.nl.my.project.profile"
        android:summaryColumn="data2"
        android:detailColumn="data3"
        android:detailSocialSummary="true" />
</ContactsSource>

Logcat:

09-23 09:09:23.981: ERROR/DatabaseUtils(223): Writing exception to parcel
09-23 09:09:23.981: ERROR/DatabaseUtils(223): java.lang.NullPointerException
09-23 09:09:23.981: ERROR/DatabaseUtils(223):     at com.android.providers.contacts.ContactsProvider2.insertData(ContactsProvider2.java:2574)
09-23 09:09:23.981: ERROR/DatabaseUtils(223):     at com.android.providers.contacts.ContactsProvider2.insertInTransaction(ContactsProvider2.java:2422)
09-23 09:09:23.981: ERROR/DatabaseUtils(223):     at com.android.providers.contacts.SQLiteContentProvider.insert(SQLiteContentProvider.java:106)
09-23 09:09:23.981: ERROR/DatabaseUtils(223):     at com.android.providers.contacts.ContactsProvider2.insert(ContactsProvider2.java:2238)
09-23 09:09:23.981: ERROR/DatabaseUtils(223):     at android.content.ContentProviderOperation.apply(ContentProviderOperation.java:214)
09-23 09:09:23.981: ERROR/DatabaseUtils(223):     at com.android.providers.contacts.SQLiteContentProvider.applyBatch(SQLiteContentProvider.java:216)
09-23 09:09:23.981: ERROR/DatabaseUtils(223):     at com.android.providers.contacts.ContactsProvider2.applyBatch(ContactsProvider2.java:2272)
09-23 09:09:23.981: ERROR/DatabaseUtils(223):     at android.content.ContentProvider$Transport.applyBatch(ContentProvider.java:193)
09-23 09:09:23.981: ERROR/DatabaseUtils(223):     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:173)
09-23 09:09:23.981: ERROR/DatabaseUtils(223):     at android.os.Binder.execTransact(Binder.java:288)
09-23 09:09:23.981: ERROR/DatabaseUtils(223):     at dalvik.system.NativeStart.run(Native Method)

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

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

发布评论

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

评论(2

悸初 2024-12-13 06:37:01

更改:

Builder builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);

至:

Builder builder = ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI);

似乎修复了 NullPointerException。

Changing:

Builder builder = ContentProviderOperation.newInsert(ContactsContract.Data.CONTENT_URI);

To:

Builder builder = ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI);

Seems to fix the NullPointerException.

尝试像这样工作

ArrayList ops = new ArrayList();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
。建造());

try working like this

ArrayList ops = new ArrayList();
ops.add(ContentProviderOperation.newInsert(ContactsContract.RawContacts.CONTENT_URI)
.withValue(ContactsContract.RawContacts.ACCOUNT_TYPE, null)
.withValue(ContactsContract.RawContacts.ACCOUNT_NAME, null)
.build());

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