CursorIndexOutOfBoundsException: индекс -1 запрошен, с размером 163 - PullRequest
0 голосов
/ 12 июня 2019

Я работаю над одним приложением, таким как видеоплеер.Я получаю 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)

1 Ответ

0 голосов
/ 12 июня 2019

Я понял это после изменения кода метода, как показано ниже.

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);
}

Спасибо за помощь, ребята.

...