安卓:java.lang.IllegalStateException:从0行山坳得到场插槽-1失败 [英] Android:java.lang.IllegalStateException: get field slot from row 0 col -1 failed

查看:109
本文介绍了安卓:java.lang.IllegalStateException:从0行山坳得到场插槽-1失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出一个Android应用程序的数据库中,当我从数据库中使用游标读取值
它给了我下面的异常。

  java.lang.IllegalStateException:从0行山坳场插槽-1失败

我用下面的code用于读取:

 公共光标fetchChildren(字符串键_){           光标C = db.rawQuery(选择儿童+ DATABASE_TABLE_NAVIGATION
                   +WHERE KEY_ =?,新的String [] {KEY_});
               返回℃;
           }     尝试{
     对于(INT K = 0; K< N; k ++){
       db.open();
     的System.out.println(孩子们);
      光标光标= db.fetchChildren(keys_a);
      如果(cursor.moveToFirst())//数据?
            {
        的System.out.println(cursor.getString(9));
     }
    }
 db.close();
    }
   赶上(例外五)
  {
  的System.out.println(E);
      }


解决方案

使用的System.out.println(cursor.getString(0))而不是的System.out.println(cursor.getString(9));

或者使用的System.out.println(cursor.getString(cursor.getColumnIndex(孩子));

的问题在你的逻辑:

您从数据库中获取单个列。所以这时光标只有一个列(儿童)。的正如你知道,列索引从0开始(0为第一列,1第2列...),你必须在 0传递的getString()。另一种方法是,搜索列索引光标和传递与的getString()。为<一个href=\"http://developer.android.com/reference/android/database/Cursor.html#getColumnIndex%28java.lang.String%29\"相对=nofollow> getColumnIndex(java.lang.String中)计算光标列名的索引和的的getString(INT)

希望这会有所帮助。

I am making an android database app in which when i fetch values from database using cursor It gives me the following exception.

java.lang.IllegalStateException: get field slot from row 0 col -1 failed

I am using the following code for fetching:

 public Cursor fetchChildren(String KEY_){

           Cursor c = db.rawQuery("SELECT children FROM " + DATABASE_TABLE_NAVIGATION
                   + " WHERE key_ = ?", new String[] {KEY_});
               return c;
           }



     try{   
     for(int k=0;k<n;k++){
       db.open();
     System.out.println("children are");
      Cursor cursor=db.fetchChildren(keys_a);
      if (cursor.moveToFirst()) // data?
            {
        System.out.println( cursor.getString(9)); 
     }
    }
 db.close();
    }
   catch(Exception e)
  {
  System.out.println(e);
      }

解决方案

Use System.out.println( cursor.getString(0)) instead of System.out.println( cursor.getString(9));

Or use System.out.println(cursor.getString(cursor.getColumnIndex("children"));

Problem in your logic:

You are fetching single column from your data base. So cursor having only one column (children). As you know that column index starts from 0 (0 for first column, 1 for 2nd column ...), you have to pass 0 in getString(). Alternate way is, to search column index in cursor and pass that with getString(). as getColumnIndex(java.lang.String) calculate the index of column name in cursor, and use it within getString(int).

Hope this will help.

这篇关于安卓:java.lang.IllegalStateException:从0行山坳得到场插槽-1失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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