光标索引超出界限错误Android?

arb*_*erb 3 sqlite android

嘿我的代码中的索引越界错误了.

来自活动:

            Cursor cursor = mDbHelper.fetchA(b);
            @SuppressWarnings("static-access")
            int a = (int) cursor.getDouble(cursor.getColumnIndex(mDbHelper.KEY_I));
            cursor.close();
Run Code Online (Sandbox Code Playgroud)

我的DB适配器:

public Cursor fetchA(String b){
    return mDb.query(DATABASE_TABLE, new String[] {KEY_I}, "b=\""+B+"\"", null, null, null, null);
}
Run Code Online (Sandbox Code Playgroud)

错误:

    E/AndroidRuntime( 5766): FATAL EXCEPTION: main

    E/AndroidRuntime( 5766): android.database.CursorIndexOutOfBoundsException: Index

     -1 requested, with a size of 1
Run Code Online (Sandbox Code Playgroud)

我怎样才能解决这个问题?

Mat*_*all 5

游标开始指向第一行之前的位置.因此,您需要将光标移动到第一行才能获取数据.Cursor#moveToFirst()或者#moveToNext()应该做这个工作.

Cursor cursor = mDbHelper.fetchA(b);
cursor.moveToNext();
@SuppressWarnings("static-access")
int a = (int) cursor.getDouble(cursor.getColumnIndex(mDbHelper.KEY_I));
cursor.close();
Run Code Online (Sandbox Code Playgroud)