我有一个游标,它根据输入的关键字查询 MediaStore。
Cursor cursor = getContentResolver().query(
uri,
projection,
where,
params,
MediaStore.Audio.Media.TITLE);
我可以毫无问题地访问 ID、标题、艺术家等,但是每当我查询 .DATA 行时,我都会收到一条错误消息,指出我应该确保在尝试从中访问数据之前正确初始化光标。
if(cursor.moveToFirst()){
String title = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE));
String artist = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST));
/** ERROR IN THIS LINE **/ String data = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA));
test.setText(title +" " + artist + " " + data);
cursor.close();
}
日志猫:
08-14 14:08:43.795: E/AndroidRuntime(4428): Caused by: java.lang.IllegalStateException: Couldn't read row 1, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
08-14 14:08:43.795: E/AndroidRuntime(4428): at android.database.CursorWindow.nativeGetString(Native Method)
08-14 14:08:43.795: E/AndroidRuntime(4428): at android.database.CursorWindow.getString(CursorWindow.java:434)
08-14 14:08:43.795: E/AndroidRuntime(4428): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
08-14 14:08:43.795: E/AndroidRuntime(4428): at android.database.CursorWrapper.getString(CursorWrapper.java:114)
它指向我活动的那条线。
如果我删除字符串数据,我会完美地得到标题、专辑名称、艺术家姓名、歌曲 ID 等。