如何解决CursorIndexOutOfBoundsException

时间:2010-12-09 09:24:56

标签: android

现在我正在使用sqllite工作json应用程序存储所有json值。值正确存储,我正在获取sqllite数据,发现错误,请帮助我...

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)

这是我的来源:

                  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();

           }

3 个答案:

答案 0 :(得分:24)

你做

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

}while(cursor.moveToNext());

你可以做到

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

}

光标位于索引-1的位置

答案 1 :(得分:4)

你的Cursor“curser”是什么定义的?你能发帖吗

编辑: 正如我所看到的,你的第二个游标dbcur没有被放置到任何位置,所以它位于他的顶部位置-1。请尝试将其与dbcur.moveToFirst()dbcur.moveToNext()

放在一起

答案 2 :(得分:0)

我只选择状态值。所以只有一个光标就足够....下面的代码工作正确........

正确的代码:

Cursor dbcur = myDB.rawQuery("select * from "+TABLE_NAME+" where Status='"+items[i]+"'", null);
                 if(dbcur.moveToFirst())
                  {
                      int k=0;
                      System.out.println("enter first");
                      do
                        {

                         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(dbcur.moveToNext());
                  }
                     dbcur.close();
                     myDB.close();