java.lang.UnsupportedOperationException

刚才在使用事务添加联系人时,报了如下的错误信息:

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 写错了;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值