Error: Make sure the Cursor is initialized correctly before accessing data from it?

匿名 (未验证) 提交于 2019-12-03 01:14:02

问题:

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 

It gives me the following error:

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

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

The database something like this:

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

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.

回答1:

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

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

to

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!