当前位置: 首页 > 面试题库 >

错误:在从游标访问数据之前,请确保游标已正确初始化?

楚彦
2023-03-14
问题内容

我已经将游标初始化如下:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //...Code, code, code...

        c = db.query("US_States", null, null, null, null, null, null, null);


    }

游标本身在同一活动中的单独方法中使用:

public void GameStart()
    {
        int gameCount = 0;
        while(gameCount < 5)
        {
            cursorEntry = new Random().nextInt(c.getCount());
            c.moveToPosition(cursorEntry);
            cursorState = c.getString(1);
            cursorCapital = c.getString(2);
            displayText.setText(cursorState);

它给了我以下错误:

E/CursorWindow锕� Failed to read row 20, column 2 from a CursorWindow which has 50 rows, 2 columns.

cursorCapital = c.getString(2);每次我重新运行应用程序时,堆栈跟踪都指向此行。那里总是会给出一个错误。

该数据库是这样的:

State|Capital
Alabama|Montgomery
Alaska|Juneau
Arizona|Phoenix
...The rest of the states

我在SO上阅读了几篇类似的文章,但它们并没有使我知道出了什么问题。任何输入表示赞赏。


问题答案:

游标的列索引从零开始,因此请更改:

 cursorState = c.getString(1);
 cursorCapital = c.getString(2);

cursorState = c.getString(0);
cursorCapital = c.getString(1);


 类似资料: