Android开发:报错Index -1 requested, with a size of 1
2016-04-28 21:17
375 查看
使用Cursor使,读取里面的数据用到getColumnIndex()时报错:
Index -1 requested, with a size of 1
仔细阅读过Cursor的文档,发现关于getColumnIndex()这个方法的说明如下:
Returns the zero-based index for the given column name, or -1 if the column doesn't exist. If you expect the column to exist use
error more clear.
文档里清楚的表明:在要读取的列不存在的时候该方法会返回值“-1”。所以可知,以上报错可能是因为要get的列不存在,也可能是因为游标位置不对。后来发现,因为我在执行这个语句前没有执行“Cursor.moveToNext();”这个函数,导致游标还位于第一位置的前面,所以索引显示为“-1”,前面加上这句就没错了。
网上另有一种情况是用完Cursor没有关闭导致报此错误,所以也提醒各位用完Cursor要记得执行Cursor.close();
Index -1 requested, with a size of 1
仔细阅读过Cursor的文档,发现关于getColumnIndex()这个方法的说明如下:
public abstract int getColumnIndex (String columnName)
Since: API Level 1Returns the zero-based index for the given column name, or -1 if the column doesn't exist. If you expect the column to exist use
getColumnIndexOrThrow(String)instead, which will make the
error more clear.
文档里清楚的表明:在要读取的列不存在的时候该方法会返回值“-1”。所以可知,以上报错可能是因为要get的列不存在,也可能是因为游标位置不对。后来发现,因为我在执行这个语句前没有执行“Cursor.moveToNext();”这个函数,导致游标还位于第一位置的前面,所以索引显示为“-1”,前面加上这句就没错了。
网上另有一种情况是用完Cursor没有关闭导致报此错误,所以也提醒各位用完Cursor要记得执行Cursor.close();
相关文章推荐
- iOS 自定义UIDatePicker日期选择器视图 —— HERO博客
- UIScrollView在Autolayout中的使用
- iOS学习笔记------UIButton、UIImageView、UILabel的属性与方法
- Builds failing with OutOfMemoryErrors(Heap or Permgen space) on Jenkins
- 对石家庄铁道大学网站首页进行UI分析
- 针对石家庄铁道大学官网首页的UI分析
- 对石家庄铁道大学官网UI设计的分析
- JAVA设计模式(04):创建型-建造模式(Builder)
- arm交叉编译器gnueabi、none-eabi、arm-eabi、gnueabihf、gnueabi区别
- iOS 自定义UIPickerView地区选择器视图 —— HERO博客
- UIViewContentMode 图文解说
- 共同学习Java源代码--常用工具类--AbstractStringBuilder(九)
- Android官方Training阅读笔记 ---- Building a Dynamic UI with Fragments(Building a Flexible UI) (二)
- iOS UIScrollView简介及使用技巧
- 共同学习Java源代码--常用工具类--AbstractStringBuilder(八)
- android 的activity 与IOS UIControllerView对比
- Android酷炫实用的开源框架(UI框架)
- Integer.valueOf(int i)与自动拆箱与装箱
- css font-style:italic与oblique的区别
- java中stringBuilder的用法