CursorWindow: Failed to read row 0, column -1 from a CursorWindow which has 7 rows, 1 columns.
java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.
Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetLong(Native Method)
at android.database.CursorWindow.getLong(CursorWindow.java:511)
at android.database.CursorWindow.getInt(CursorWindow.java:578)
at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:84)
at com.image.utils.LocalImageHelper.queryAllFavouritesImg(LocalImageHelper.java:765)
at com.image.activity.PhotoAlbumDetailActivity2$1$1.run(PhotoAlbumDetailActivity2.java:160)
在查询数据库的时候遇到Couldn't read row 0, col -1 from CursorWindow这个异常。应该是行写错了,不过查看了并没写错。代码如下:
/**
* 图片收藏sql语句
*/
public static final String TAB_FAVOURITES= "Tab_Favourites";
public static final String FAV_IMG_ID= "img_id";
public static final String FAV_IMAGE_URI= "image_uri";
public static final String FAV_ID= "fav_id";
//图片收藏
public static final String CREATE_PHOTO_FAVOURITES = "create table if not exists "+TAB_FAVOURITES
+ "(fav_id integer primary key autoincrement,"
+ "img_id integer,"
+ "image_uri text)";
//查询所有收藏的图片
public List<LocalFile> queryAllFavouritesImg(){
List<LocalFile> imgList = new ArrayList<>();
LocalFile localFile;
Cursor cursor = db.rawQuery("select " + FAV_IMAGE_URI + " from " + TAB_FAVOURITES + " order by " + FAV_ID+" desc", null);
if(cursor !=null&&cursor.moveToFirst()&&cursor.getCount()>0) {
do {
localFile = new LocalFile();//localFile一定要声明在do-while里,否则是同一个值
localFile.setOriginalUri(cursor.getString(cursor.getColumnIndex(FAV_IMAGE_URI)));
localFile.setMediaID(cursor.getInt(cursor.getColumnIndex(FAV_IMG_ID)));
imgList.add(localFile);
Log.e("imgList","==========="+imgList.toString());
} while (cursor.moveToNext());
}
cursor.close();
return imgList;
}
名字都没写错,也没有空行之类的问题,cursor.getInt也没写错。网上搜了资料,没发现有错误的地方。
后来想到,我要查询的是image_uri和img_id,但是我查询语句只写了select image_uri,但是get值的时候除了get了image_uri,也get了img_id。会不会是因为没查询img_id的值?然后修改了查询语句,新的语句如下:
Cursor cursor = db.rawQuery("select " + FAV_IMAGE_URI + ","+FAV_IMG_ID+" from " + TAB_FAVOURITES + " order by " + FAV_ID+" desc", null);
问题解决。
后来我又遇到这个问题,错误原因是double和float混用,存入的是float,取的时候用的double类型,出现了错误。统一为float解决了。
下为转载,总结的比较好:
常见的错误原因解决:
错误1:请求的字段在数据库的表中不存在,一般是大小写没写对。
错误2:编程的中途改变表的字段,实际字段并没有改变,解决方法是卸载当前版本,再安装调试。
错误3:查询语句中并没有查询该字段,使用的时候却要得到该字段的值。
下边还有一种不常见的错误,查了好多资料都没有查到,很少人遇到一样,记录下来希望可以帮到别人:
错误4:如果字段中有Blob类型的,存储的文件最好不要超过1MB,超过1MB就容易出现上述错误。
解决办法就是要么压缩,要么分批读取出来,(韩国朋友的代码,个人没有测试,仅供大家参考思路:)
http://www.cnblogs.com/channe/p/3435836.html