代碼:NullPointerException異常上CustomContact插入
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)
需要等待2天才能接受我自己的帳篷。 –