如何解决CursorIndexOutOfBoundsException [英] how to solve CursorIndexOutOfBoundsException

查看:213
本文介绍了如何解决CursorIndexOutOfBoundsException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我正在使用sqllite所有存储的JSON values​​.The值JSON应用程序被正确地存储和我获取的数据下面的错误sqllite的发生,请帮我...

  12-09 13:51:22.808:ERROR / AndroidRuntime(217):未捕获的处理程序:螺纹主力退出,由于未捕获的异常
12-09 13:51:22.808:ERROR / AndroidRuntime(217):android.database.CursorIndexOutOfBoundsException:指数-1要求,为5的尺寸
12-09 13:51:22.808:ERROR / AndroidRuntime(217):在android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
12-09 13:51:22.808:ERROR / AndroidRuntime(217):在android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:172)
12-09 13:51:22.808:ERROR / AndroidRuntime(217):在android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
12-09 13:51:22.808:ERROR / AndroidRuntime(217):在ez.com.Action_module_screen.setListval1(Action_module_screen.java:1059)
12-09 13:51:22.808:ERROR / AndroidRuntime(217):在ez.com.Action_module_screen $ 4.handleMessage(Action_module_screen.java:695)
12-09 13:51:22.808:ERROR / AndroidRuntime(217):在android.os.Handler.dispatchMessage(Handler.java:99)
12-09 13:51:22.808:ERROR / AndroidRuntime(217):在android.os.Looper.loop(Looper.java:123)
12-09 13:51:22.808:ERROR / AndroidRuntime(217):在android.app.ActivityThread.main(ActivityThread.java:4203)
12-09 13:51:22.808:ERROR / AndroidRuntime(217):在java.lang.reflect.Method.invokeNative(本机方法)
12-09 13:51:22.808:ERROR / AndroidRuntime(217):在java.lang.reflect.Method.invoke(Method.java:521)
12-09 13:51:22.808:ERROR / AndroidRuntime(217):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:791)
12-09 13:51:22.808:ERROR / AndroidRuntime(217):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
12-09 13:51:22.808:ERROR / AndroidRuntime(217):在dalvik.system.NativeStart.main(本机方法)
 

这是我的源:

 光标光标= myDB.rawQuery(SELECT * FROM+ TABLE_NAME +,NULL);
 如果(curser.moveToFirst())
     {
      INT K = 0;
      的System.out.println(输入第一);
      做
        {
      的System.out.println(进入第二个);
      如果(STATUS1 ==真)
      {
      的System.out.println(进入第三个);
      的for(int i = 0; I< items.length;我++)
       {
      的System.out.println(项目+ items.length);
      的System.out.println(进入第四);
      光标dbcur = myDB.rawQuery(SELECT * FROM+ TABLE_NAME +在哪里状态='+项目[我] +,NULL);
      的System.out.println(进入第五);
      INT TITLE = curser.getColumnIndex(标题);
      字符串针锋相对= curser.getString(职称);
      的System.out.println(值+针锋相对);

     / *字符串TITLE1 = dbcur.getString(dbcur.getColumnIndex(标题));
      的System.out.println(标题+ TITLE1);
      字符串名称1 = dbcur.getString(dbcur.getColumnIndex(名称));
      的System.out.println(姓名+名1);
      字符串OPEN1 = dbcur.getString(dbcur.getColumnIndex(打开));
      的System.out.println(开放+ OPEN1);
      字符串close1 = dbcur.getString(dbcur.getColumnIndex(关闭));
      的System.out.println(关闭+ close1);
      串NO1 = dbcur.getString(dbcur.getColumnIndex(否));
      的System.out.println(否+ NO1);
      no.add(NO1 +,+ K);
   first.put(NO1 +,+ K,TITLE1);
   second.put(NO1 +,+ K,NAME1);
   third.put(NO1 +,+ K,OPEN1);
   fourth.put(NO1 +,+ K,close1);
   K = K + 1;
   mylist中=分类(无,1,真正的); * /

     }

       }


        }而(curser.moveToNext());
     }
    curser.close();
  myDB.close();

           }
 

解决方案

 如果(cursor.moveToFirst()及和放大器; cursor.getCount()> = 1){
  做{
    ....
        ....

}而(cursor.moveToNext());
 

您可以做

 如果(cursor.getCount()> = 1){
  而(cursor.moveToNext()){
        ....
        ....

}
 

的光标位置指数-1 intially

Now I am working json Application using sqllite for stored all json values.The values are stored correctly and i am fetching data for sqllite below error is occurred please help me...

12-09 13:51:22.808: ERROR/AndroidRuntime(217): Uncaught handler: thread main exiting due to uncaught exception
12-09 13:51:22.808: ERROR/AndroidRuntime(217): android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 5
12-09 13:51:22.808: ERROR/AndroidRuntime(217):     at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
12-09 13:51:22.808: ERROR/AndroidRuntime(217):     at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:172)
12-09 13:51:22.808: ERROR/AndroidRuntime(217):     at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
12-09 13:51:22.808: ERROR/AndroidRuntime(217):     at ez.com.Action_module_screen.setListval1(Action_module_screen.java:1059)
12-09 13:51:22.808: ERROR/AndroidRuntime(217):     at ez.com.Action_module_screen$4.handleMessage(Action_module_screen.java:695)
12-09 13:51:22.808: ERROR/AndroidRuntime(217):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-09 13:51:22.808: ERROR/AndroidRuntime(217):     at android.os.Looper.loop(Looper.java:123)
12-09 13:51:22.808: ERROR/AndroidRuntime(217):     at android.app.ActivityThread.main(ActivityThread.java:4203)
12-09 13:51:22.808: ERROR/AndroidRuntime(217):     at java.lang.reflect.Method.invokeNative(Native Method)
12-09 13:51:22.808: ERROR/AndroidRuntime(217):     at java.lang.reflect.Method.invoke(Method.java:521)
12-09 13:51:22.808: ERROR/AndroidRuntime(217):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
12-09 13:51:22.808: ERROR/AndroidRuntime(217):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:549)
12-09 13:51:22.808: ERROR/AndroidRuntime(217):     at dalvik.system.NativeStart.main(Native Method)

This is my source:

                  Cursor curser=myDB.rawQuery("select * from "+TABLE_NAME+"",null); 
 if(curser.moveToFirst())
     {
      int k=0;
      System.out.println("enter first");
      do
        {
      System.out.println("enter second");
      if(status1==true)
      {
      System.out.println("enter third"); 
      for(int i=0;i<items.length;i++)
       {
      System.out.println("item"+items.length);
      System.out.println("enter fourth");
      Cursor dbcur = myDB.rawQuery("select * from "+TABLE_NAME+" where Status='"+items[i]+"'", null);
      System.out.println("enter fifth");
      int title=curser.getColumnIndex("Title");
      String tit=curser.getString(title);
      System.out.println("value"+tit);

     /* String title1=dbcur.getString(dbcur.getColumnIndex("Title"));
      System.out.println("title"+title1);
      String name1=dbcur.getString(dbcur.getColumnIndex("Name"));
      System.out.println("name"+name1);
      String open1=dbcur.getString(dbcur.getColumnIndex("Open"));
      System.out.println("open"+open1);
      String close1=dbcur.getString(dbcur.getColumnIndex("Close"));
      System.out.println("close"+close1);
      String no1=dbcur.getString(dbcur.getColumnIndex("No"));
      System.out.println("no"+no1);
      no.add(no1+","+k);
   first.put(no1+","+k, title1);
   second.put(no1+","+k,name1);
   third.put(no1+","+k, open1);
   fourth.put(no1+","+k,close1);
   k=k+1; 
   mylist=sorting(no,1,true);  */

     }

       }


        }while(curser.moveToNext());
     }
    curser.close();
  myDB.close();

           }

解决方案

Either you do

if(cursor.moveToFirst() && cursor.getCount() >= 1){
  do{
    ....
        ....

}while(cursor.moveToNext());

OR you can do

if(cursor.getCount() >= 1){
  while(cursor.moveToNext()){
        ....
        ....

}

The cursor is position at index -1 intially

这篇关于如何解决CursorIndexOutOfBoundsException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆