本文介绍了CursorIndexOutOfBoundsException索引0请求,大小为0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从数据库的某一行获取文本。为此,我创建了这个函数

I'm trying to get a text from a certain row of my database. For this, I made this function

public String getTranslation(long rowId) throws SQLException {
    String str = null;
    Cursor mCursor = db.query(
        true, TABLE_LANGUAGE_FR, new String[] {
            KEY_ID_INTERFACE_TEXT, KEY_ITEM_NAME,KEY_FORM_LABEL_ID, KEY_FORM_RANK
        },
        KEY_FORM_LABEL_ID + "=" + rowId, null, null, null, null, null
    );

    if (mCursor != null) {
        mCursor.moveToFirst();
        str = mCursor.getString(mCursor.getColumnIndex(KEY_ITEM_NAME));
    }
    return str;
}

,我这样调用:

db = new DbAdapter(this);
db.createDatabase();
db.openDataBase();
String str = db.getTranslation(1706);

我的表格如下:

我收到此错误:

09:32:08.965    307 com.Orange  ERROR   AndroidRuntime  Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
09:32:08.965    307 com.Orange  ERROR   AndroidRuntime      at android.database.AbstractCursor.checkPosition(AbstractCursor.java:580)
09:32:08.965    307 com.Orange  ERROR   AndroidRuntime      at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
09:32:08.965    307 com.Orange  ERROR   AndroidRuntime      at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
09:32:08.965    307 com.Orange  ERROR   AndroidRuntime      at com.Orange.Database.DbAdapter.getTranslation(DbAdapter.java:141)
09:32:08.965    307 com.Orange  ERROR   AndroidRuntime      at com.Orange.Authentication.onCreate(Authentication.java:32)

为什么会收到此错误?我在这里做错了什么?

Why am I getting this error? What am I doing wrong here?

提前感谢。

推荐答案

看起来你有一个emtpy游标。而不是检查它是否为空尝试检查

It would seem like you have an emtpy cursor. Instead of checking whether it is null try checkcing

if(mCursor.getCount() > 0)
{
  //do stuff
}

尝试移除您的位置条件,实际上在运行select语句时获取数据。
如果你仍然没有任何东西,你可能在你的tablenames有一个错字,或者连接/创建数据库/表失败。

try removing your where condition to make sure you actually get data when running the select statement.If you still get nothing you might have a typo in your tablenames, or failed to connect/create the database/tables.

Cursor mCursor = db.query("sys_interface_text", new String[] {
       "id_interface_text", "item_name","form_label_id", "form_rank"},
               "form_label_id = 1706", null, null, null,
       null);

这篇关于CursorIndexOutOfBoundsException索引0请求,大小为0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 07:29