我正在开发一个像视频播放器这样的应用程序。当执行特定方法时,我得到CursorIndexOutOfBoundsException。我要在此处发布该方法。
代码:
public void getVideoBuckets() {
Cursor cursor = getContentResolver().query(Media.EXTERNAL_CONTENT_URI, this.bucketProjection, null, null, "date_added");
ArrayList<String> bucketNamesTEMP = new ArrayList<>(cursor.getCount());
ArrayList<String> bucketImageTemp = new ArrayList<>(cursor.getCount());
HashSet<String> albumSet = new HashSet<>();
if (cursor.moveToLast()) {
while (!Thread.interrupted()) {
String album = cursor.getString(cursor.getColumnIndex(this.bucketProjection[0]));
String image = cursor.getString(cursor.getColumnIndex(this.bucketProjection[1]));
//Issue with above two lines.
if (image != null && new File(image).exists() && !albumSet.contains(album)) {
bucketNamesTEMP.add(album);
bucketImageTemp.add(image);
albumSet.add(album);
}
}
return;
}
cursor.close();
if (bucketNamesTEMP == null) {
this.bucketNamesList = new ArrayList();
}
this.bucketNamesList.clear();
this.bucketImagePathList.clear();
this.bucketNamesList.addAll(bucketNamesTEMP);
this.bucketImagePathList.addAll(bucketImageTemp);
}
从logcat中崩溃的详细信息:
Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 163
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:466)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at android.database.CursorWrapper.getString(CursorWrapper.java:137)
at com.andromania.MyVideoInputGallery.BucketActivity.getVideoBuckets(BucketActivity.java:168)
答案 0 :(得分:0)
在更改如下方法的代码后,我解决了该问题。
public void getVideoBuckets() {
final Cursor query = this.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, this.bucketProjection, (String)null, (String[])null, "date_added");
final ArrayList list = new ArrayList<String>(query.getCount());
final ArrayList list2 = new ArrayList<String>(query.getCount());
final HashSet<String> set = new HashSet<String>();
MyLabel: {
if (!query.moveToLast()) {
break MyLabel;
}
while (!Thread.interrupted()) {
final String string = query.getString(query.getColumnIndex(this.bucketProjection[0]));
final String string2 = query.getString(query.getColumnIndex(this.bucketProjection[1]));
if (string2 != null && new File(string2).exists() && !set.contains(string)) {
list.add(string);
list2.add(string2);
set.add(string);
}
if (!query.moveToPrevious()) {
break MyLabel;
}
}
return;
}
query.close();
if (list == null) {
this.bucketNamesList = new ArrayList();
}
this.bucketNamesList.clear();
this.bucketImagePathList.clear();
this.bucketNamesList.addAll(list);
this.bucketImagePathList.addAll(list2);
}
感谢帮助人员。