ArrayIndexOutOfBounds数组中大量对象的异常

时间:2018-05-29 10:12:49

标签: java android indexoutofboundsexception

当数据大小为600时,没有优势。 当数据大小为800时,我在完成所有迭代并退出以下行的循环后会遇到异常:

'imageAdapter = new ImageAdapter(MainActivity.getAppContext(), posters);'

问题与循环内部的splitArray []无关。请注意,它适用于较小尺寸的数据。 请帮忙。

FATAL EXCEPTION: main
Process: com.udacityproject.svs.popularmovies, PID: 12517
java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
at com.udacityproject.svs.popularmovies.MainActivity$1.deliverResult(MainActivity.java:130)
at com.udacityproject.svs.popularmovies.MainActivity$1.deliverResult(MainActivity.java:57)
at android.support.v4.content.AsyncTaskLoader.dispatchOnLoadComplete(AsyncTaskLoader.java:255)
at android.support.v4.content.AsyncTaskLoader$LoadTask.onPostExecute(AsyncTaskLoader.java:80)
at android.support.v4.content.ModernAsyncTask.finish(ModernAsyncTask.java:487)
at android.support.v4.content.ModernAsyncTask$InternalHandler.handleMessage(ModernAsyncTask.java:504)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

图像适配器的构造函数:

    public ImageAdapter(@NonNull Context context, @NonNull Poster[] objects){
        super(context, 0, objects);
        posters = objects;
    }

功能:

    public void deliverResult(String[] data) {

        if (data != null) {
            Poster[] posters = new Poster[data.length];
            for (int i=0; i<data.length; i++) {
                String imagePath = "http://image.tmdb.org/t/p/w185/" ;
                int imageId;
                String title;
                String overview;
                String rating;
                String releaseDate;
                String[] splitArray;
                try {
                    splitArray = data[i].split("[*]");
                } catch (PatternSyntaxException e) {
                    e.printStackTrace();
                    return;
                }
                imagePath+=splitArray[2];
                imageId = Integer.valueOf(splitArray[0]);
                title = splitArray[1];
                rating = splitArray[3];
                overview = splitArray[4];
                releaseDate = splitArray[5];
                posters[i] = new Poster( imageId, imagePath, title, overview, rating, releaseDate);
            }
            imageAdapter = new ImageAdapter(MainActivity.getAppContext(), posters);
            mMoviePoster.setAdapter(imageAdapter);
        }

    }

1 个答案:

答案 0 :(得分:2)

第600行和第800行之间的一行不完整。

添加以下行:

assert splitArray.length > 4 : "Following line lacks a column: " + data[i];

之后:

splitArray = data[i].split("[*]");

它应该告诉你哪一行缺少第五列