我有一个光标,根据输入的关键字查询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();
}
Logcat:
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等。
答案 0 :(得分:0)
抱歉,我修好了。我的查询投影中没有Mediastore.Audio.Media.DATA。
这可能留给将来需要它的任何人。