错误:确保光标从它访问数据之前,正确初始化? [英] Error: Make sure the Cursor is initialized correctly before accessing data from it?

查看:251
本文介绍了错误:确保光标从它访问数据之前,正确初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经光标初始化如下:

I have cursor initialized as follows:

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

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


    }

本身相同的活性中使用在一个独立的方法中的光标

The cursor itself is used in a separate method within the same activity:

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

这使我有以下错误:

It gives me the following error:

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

通过堆栈跟踪指向该行 cursorCapital = c.getString(2); 每次我重新运行该应用程序。它总是给人一个错误出现。

With a stack trace pointing at this line cursorCapital = c.getString(2); every time I rerun the application. It always gives an error there.

该数据库是这样的:

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

我看过一对夫妇这样类似的职位,但他们并没有给我什么错误的想法。任何输入AP preciated。

I read a couple of similar posts on SO, but they didn't give me an idea of what is going wrong. Any input is appreciated.

推荐答案

光标的列索引是从零开始的,所以改变:

The column index of a cursor is zero-based, so change:

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

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

这篇关于错误:确保光标从它访问数据之前,正确初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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