Android cursor error - “make sure cursor is initialized correctly before accessing data from it…”

最后都变了- 提交于 2019-11-28 09:14:25

The problem isn't in the row, it's in the column.

Couldn't read row 3, **col -1** from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

It's basically saying that your MediaColumns.TITLE column doesn't exist in the cursor. Which is true. It's not in your first cursor (the one that it is referencing). Your other cursors are all within if statements so go out of scope and leave only the first one.

You can either re-pull the cursor like you do in the other portions of the if statement, or find some way to persist the cursor you got from the last if statement.

EDIT

It's pretty simple to fix, make the cursor a class variable. Also, I wouldn't keep re-using "cursor". Label them somethign individual and descriptive, it'll help you maintain readability in your code. I might do it like this:

public class MusicFlipper extends Activity implements OnItemClickListener {
    private Cursor artistCursor;
    private Cursor albumCursor;

Then you call them like you were but use the individual names.

albumCursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                columns, where, whereVal, orderBy);

Since you declared it as a class variable it will be available through the whole class so in the last part you'd do:

if (currentList.equals("Songs")) {
    if (albumCursor.moveToPosition(position)) {
            String title = albumCursor.getString(albumCursor.getColumnIndex(MediaColumns.TITLE));
            TextView myTextView = (TextView) findViewById(R.id.title);
            myTextView.setText(title);
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!