刚才在使用事务添加联系人时,报了如下的错误信息:
java.lang.UnsupportedOperationException: Aggregate contacts are created automatically
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:146)
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:114)
at android.content.ContentProviderProxy.insert(ContentProviderNative.java:408)
at android.content.ContentResolver.insert(ContentResolver.java:604)
at cn.jbit.Contact.test.TestVisitContacts.testAddContact(TestVisitContacts.java:71)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)
另外testAddContactsByTransaction() 方法进行单元测试时,报了如下的错误消息:
java.lang.UnsupportedOperationException: URI: content://com.android.contacts/raw_contants, calling user: cn.jbit.Contact, calling package:cn.jbit.Contact
at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:146)
at android.database.DatabaseUtils.readExceptionWithOperationApplicationExceptionFromParcel(DatabaseUtils.java:137)
at android.content.ContentProviderProxy.applyBatch(ContentProviderNative.java:449)
at android.content.ContentProviderClient.applyBatch(ContentProviderClient.java:95)
at android.content.ContentResolver.applyBatch(ContentResolver.java:639)
at cn.jbit.Contact.test.TestVisitContacts.testAddContactsByTransaction(TestVisitContacts.java:147)
at java.lang.reflect.Method.invokeNative(Native Method)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:529)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1448)
当时,单元测试的方法如下:
/***
* 利用事务添加 联系人 毕竟 添加的时候 是在多张表中进行添加的
* @throws Exception
* @throws RemoteException
*/
public void testAddContactsByTransaction() throws RemoteException, Exception{
ContentResolver rs =this.getContext().getContentResolver();
ArrayList<ContentProviderOperation> operations =new ArrayList<ContentProviderOperation>();
Uri uri =Uri.parse("content://com.android.contacts/raw_contants");
ContentProviderOperation op1 =ContentProviderOperation.newInsert(uri)
.withValue("account_name", null)
.build();
operations.add(op1);
Uri uri2 =Uri.parse("content://com.android.contacts/data");
ContentProviderOperation op2 =ContentProviderOperation.newInsert(uri2)
.withValueBackReference("raw_contact_id", 0) // 这里相当于 为其指定了 where 条件 当条件有多个时 就使用ContentValues
.withValue("mimetype", "vnd.android.cursor.item/name")
.withValue("data1", "张三")
.build();
operations.add(op2);
ContentProviderOperation op3 =ContentProviderOperation.newInsert(uri2)
.withValueBackReference("raw_contact_id", 0)
.withValue("mimetype", "vnd.android.cursor.item/phone_v2")
.withValue("data1", "12345678")
.build();
operations.add(op3);
ContentProviderOperation op4 =ContentProviderOperation.newInsert(uri2)
.withValueBackReference("raw_contact_id", 0)
.withValue("mimetype", "vnd.android.cursor.item/email_v2")
.withValue("data1", "zhjw009@163.com")
.build();
operations.add(op4);
rs.applyBatch("com.android.contacts", operations); // 该方法的内部结构 会 循环 提供的这些 内容提供者操作对象 并按事务的方式处理
}
当你仔细看有对应颜色匹配的代码和错误消息,不难发现:
当程序抛出 java.lang.UnsupportedOperationException异常时,基本上可以猜测是 Uri 写错了;