CursorIndexOutOfBoundsException:请求索引 -1,大小为 163
CursorIndexOutOfBoundsException: Index -1 requested, with a size of 163
我正在开发一个应用程序,例如视频播放器。当执行特定方法时,我得到 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)
我像下面这样修改方法的代码后解决了这个问题。
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);
}
感谢大家的帮助。
我正在开发一个应用程序,例如视频播放器。当执行特定方法时,我得到 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)
我像下面这样修改方法的代码后解决了这个问题。
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);
}
感谢大家的帮助。