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

来源:互联网 发布:淘宝店铺装修怎么做 编辑:程序博客网 时间:2024/05/16 05:50
描述:
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)
1 0
原创粉丝点击