doInBackground() 错误
Error in doInBackground()
我正在尝试从代码中的 5 URL 中获取值:
new JsonTask().execute(urlPosts, urlPhotos, urlComments, urlComments, urlAlbums, urlUsers);
但是,我陷入了这个 class:
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.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.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.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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)
为什么会出现这个错误?我该如何解决?谢谢。
Why am i getting this error?
您已清楚地知道原因 - 只需仔细阅读整个堆栈跟踪:
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=4; index=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
以避免固定大小的数组。
我正在尝试从代码中的 5 URL 中获取值:
new JsonTask().execute(urlPosts, urlPhotos, urlComments, urlComments, urlAlbums, urlUsers);
但是,我陷入了这个 class:
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.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.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.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.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)
为什么会出现这个错误?我该如何解决?谢谢。
Why am i getting this error?
您已清楚地知道原因 - 只需仔细阅读整个堆栈跟踪:
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=4; index=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
以避免固定大小的数组。