CustomContact 插入时出现 NullPointerException
代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
更改:
至:
似乎修复了 NullPointerException。
Changing:
To:
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());