您的位置:首页 > 产品设计 > UI/UE

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

2015-01-02 13:40 489 查看
描述:

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)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐