doInBackground()出错

时间:2017-02-24 16:41:46

标签: android json runtimeexception

我正在尝试从5 URL这里获取代码:

 new JsonTask().execute(urlPosts, urlPhotos, urlComments, urlComments, urlAlbums, urlUsers);

但是,我在这堂课中陷入困境:

protected JSONObject[] doInBackground(String... params) {

        URLConnection urlConnection = null;
        BufferedReader bufferedReader = null;
        JSONObject[] jsonObjects = new JSONObject[4];

        jsonObjects[0] = getPosts();
        jsonObjects[1] = getPhotos();
        jsonObjects[2] = getComments();
        jsonObjects[3] = getAlbums();
        jsonObjects[4] = getUsers();

        return jsonObjects;
    }

错误:

E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
              Process: com.example.marcellosouza.saberproject, PID: 12105
              java.lang.RuntimeException: An error occurred while executing doInBackground()
                  at android.os.AsyncTask$3.done(AsyncTask.java:309)
                  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                  at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                  at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                  at java.lang.Thread.run(Thread.java:818)
               Caused by: java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
                  at com.example.marcellosouza.saberproject.view.activity.MainPageBlogActivity$JsonTask.doInBackground(MainPageBlogActivity.java:102)
                  at com.example.marcellosouza.saberproject.view.activity.MainPageBlogActivity$JsonTask.doInBackground(MainPageBlogActivity.java:80)
                  at android.os.AsyncTask$2.call(AsyncTask.java:295)
                  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                  at java.lang.Thread.run(Thread.java:818) 

为什么我会收到此错误?我该如何解决?谢谢。

1 个答案:

答案 0 :(得分:1)

  

为什么我会收到此错误?

你清楚地知道为什么 - 仔细阅读整个堆栈跟踪:

  

引起:java.lang。 ArrayIndexOutOfBoundsException :length = 4;索引= 4

它甚至会告诉您,您的数组大小为4个元素(长度= 4),但您尝试到达它的外部 - > index=4。由于数组索引从0开始,因此4表示第5个元素,因此崩溃。

然后,堆栈跟踪会告诉您错误代码的位置:

  

com.example.marcellosouza.saberproject.view.activity.MainPageBlogActivity                  $ JsonTask.doInBackground(的 MainPageBlogActivity.java:102

你做错了 - 你声明了4个元素数组:

    JSONObject[] jsonObjects = new JSONObject[4];

但稍后你会尝试填充5:

    jsonObjects[0] = getPosts();
    jsonObjects[1] = getPhotos();
    jsonObjects[2] = getComments();
    jsonObjects[3] = getAlbums();
    jsonObjects[4] = getUsers();

那不行。修复jsonObjects创建或使用ArrayList以避免固定大小的数组。