问题描述
- android getContentResolver返回值的问题
-
最近学了下面的代码,运行时报错数组越界,检查发现newUriList为空,再进一步检查发现是因为getContentResolver.insert的返回值newUri的权限,路径以及id之间没有“/",求大神解释这怎么解决,谢谢![图片说明](http://img.ask.csdn.net/upload/201502/25/1424845714_34467.jpg) ![图片说明](http://img.ask.csdn.net/upload/201502/25/1424845776_174148.jpg) ![![![](http://img.ask.csdn.net/upload/201502/25/1424845714_34467.jpg)](http://img.ask.csdn.net/upload/201502/25/1424845714_34467.jpg)](http://img.ask.csdn.net/upload/201502/25/1424845714_34467.jpg) Uri uri=Uri.parse("content://com.example.databasetest.provider/book"); ContentValues values=new ContentValues(); values.put("name", "A clash of kings"); values.put("author", "Gerorge Martin"); values.put("pages", 1040); values.put("price", 22.85); android.net.Uri newUri=getContentResolver().insert(uri, values); Log.d("newUriList","newUri is "+newUri.toString()); List<String> newUriList=newUri.getPathSegments(); if(newUriList.isEmpty()){ Log.d("newUriList","newUriList is empty"); }else{ Log.d("newUriList","newUriList has "+newUriList.size()+" elements"); Log.d("newUriList","The first element of newUriList is "+newUriList.get(0)); Log.d("newUriList","The second element of newUriList is "+newUriList.get(1)); } newId=newUriList.get(1);
解决方案
最后解决了没有,我也遇到了这种情况
解决方案二:
在你自定义的Provider中,把insert操作中的Uri中加上/就可以啦
时间: 2024-09-29 16:38:29