android.database.CursorIndexOutOfBoundsException: Index 5 requested, with a size of 5

描述:
01-02 00:13:43.380: E/flyLog:ChatManager(963): getUnreadChatGroupandroid.database.CursorIndexOutOfBoundsException: Index 5 requested, with a size of 5
01-02 00:13:43.380: E/flyLog:ChatManager(963): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)
01-02 00:13:43.380: E/flyLog:ChatManager(963): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)

01-02 00:13:43.380: E/flyLog:ChatManager(963): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)


我的代码是这样的:

<span style="white-space:pre">	</span>cursor = db.rawQuery("select id,group_name,group_type from chat_group g where group_type in( "+p.toString()+" ) and exists(select 1 from user_chat_unreads c where c.group_id=g.id and c.status=0 and c.user_id=? ) order by last_time desc;", params);
			
	if(cursor.moveToFirst()){
		for (int i = 0; i < cursor.getCount(); i++) {
			cursor.move(i);
			ChatGroup chatGroup = new ChatGroup();
			String id = cursor.getString(0);
<span style="white-space:pre">		</span>}
<span style="white-space:pre">	</span>}

cursor.move是从网上拷贝过来的,并且网上都说这样取的,看不出来问题,仔细debug也没发现代码问题,于是看官方API,

Move the cursor by a relative amount, forward or backward, from the current position. Positive offsets move forwards, negative offsets move backwards. If the final position is outside of the bounds of the result set then the resultant position will be pinned to -1 or count() depending on whether the value is off the front or end of the set, respectively. 
This method will return true if the requested destination was reachable, otherwise, it returns false. For example, if the cursor is at currently on the second entry in the result set and move(-5) is called, the position will be pinned at -1, and false will be returned. 


意思是Cursor的move(i)的i是移动相对量,即相对于当前值向前或向后移动i个,i是正的就向后移动,i是负数就向前移动

我勒个去

果断改成moveToPosition(int position) ,果然好了,看来不能尽信网,不然不如无网

或参考其他方法改变代码处理方式:
moveToNext() 
 相当于move(1)
moveToPrevious() 
 相当于move(-1)
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值