android 使用游标获取 android.database.CursorIndexOutOfBoundsException:Index -1
来源:互联网 发布:手机面部解锁软件 编辑:程序博客网 时间:2024/05/16 08:16
具体报错如下 :
Thread ID: 15908 Thread Name: pool-12-thread-1 Time: 2016-09-29 17:57:39:43 FromClass: CrashHandler > ↓↓↓↓exception↓↓↓↓java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.pggmall.chatuidemo/com.pggmall.origin.activity.correcting.C_PublishPurchaseInquiryActivity_}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0at android.app.ActivityThread.deliverResults(ActivityThread.java:4734)at android.app.ActivityThread.handleSendResult(ActivityThread.java:4777)at android.app.ActivityThread.access$1500(ActivityThread.java:211)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1754)at android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:145)at android.app.ActivityThread.main(ActivityThread.java:6918)at java.lang.reflect.Method.invoke(Native Method)at java.lang.reflect.Method.invoke(Method.java:372)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)at android.database.CursorWrapper.getString(CursorWrapper.java:114)at com.pggmall.origin.activity.correcting3.upload_img.base.UpLoadActivity.onActivityResult(UpLoadActivity.java:526)at android.app.Activity.dispatchActivityResult(Activity.java:6867)at android.app.ActivityThread.deliverResults(ActivityThread.java:4730)... 10 moreandroid.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0at android.database.AbstractCursor.checkPosition(AbstractCursor.java:426)at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)at android.database.CursorWrapper.getString(CursorWrapper.java:114)at com.pggmall.origin.activity.correcting3.upload_img.base.UpLoadActivity.onActivityResult(UpLoadActivity.java:526)at android.app.Activity.dispatchActivityResult(Activity.java:6867)at android.app.ActivityThread.deliverResults(ActivityThread.java:4730)at android.app.ActivityThread.handleSendResult(ActivityThread.java:4777)at android.app.ActivityThread.access$1500(ActivityThread.java:211)at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1754)at android.os.Handler.dispatchMessage(Handler.java:102)at android.os.Looper.loop(Looper.java:145)at android.app.ActivityThread.main(ActivityThread.java:6918)at java.lang.reflect.Method.invoke(Native Method)at java.lang.reflect.Method.invoke(Method.java:372)at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
解决方法:
android中数据库处理使用cursor时,游标不是放在为0的下标,而是放在为-1的下标处开始的。
也就是说返回给cursor查询结果时,不能够马上从cursor中提取值。
下面的代码会返回错误
User u = null;
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from user where id = ?", new String[] { id.toString() });
Integer uid = cursor.getInt(cursor.getColumnIndex("id"));
String uname = cursor.getString(cursor.getColumnIndex("name"));
float uamount = cursor.getFloat(cursor.getColumnIndex("amount"));
u = new User(uid, uname, uamount);
正确的用法
User u = null;
SQLiteDatabase db = dbHelper.getReadableDatabase();
Cursor cursor = db.rawQuery("select * from user where id = ?", new String[] { id.toString() });
if (cursor.moveToFirst())
{
}
- android 使用游标获取 android.database.CursorIndexOutOfBoundsException:Index -1
- android.database.CursorIndexOutOfBoundsException:Index -1
- cursor使用错误(android.database.CursorIndexOutOfBoundsException: Index -1 requested)
- Database: android.database.CursorIndexOutOfBoundsException
- android.database.CursorIndexOutOfBoundsException
- SQLite android.database.CursorIndexOutOfBoundsException
- Android开发7——android.database.CursorIndexOutOfBoundsException:Index -1 requested, with a size of 1
- android.database.CursorIndexOutOfBoundsException:Index -1 requested, with a size of 1
- android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
- android.database.CursorIndexOutOfBoundsException:Index -1 requested, with a size of 1
- android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
- android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1
- android.database.CursorIndexOutOfBoundsException:Index -1 requested, with a size of 1
- android.database.CursorIndexOutOfBoundsException:Index -1 requested, with a size of 1
- android.database.CursorIndexOutOfBoundsException:Index -1 requested, with a size of 0
- android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 3
- Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 2
- qlite操作时出现下面错误 android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of
- JavaScript 闭包
- Java引用传递问题示例
- 为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
- tensorflow安装过程中报错:Failed to establish a new connection: [Errno 101]
- Camera实现3D翻转效果
- android 使用游标获取 android.database.CursorIndexOutOfBoundsException:Index -1
- 读写文件简单小例子,仅作自己参考
- MVP设计模式
- windows 下Zookeeper的单机集成
- 父类调用bug
- 设置popWindow出现时,背景后面变暗
- StageFright框架流程解读
- 获取emoji网页的内容保存到本地
- Mybatis源码分析之结果封装ResultSetHandler和DefaultResultSetHandler