我有一个简单的程序,可以从Google Images API获取JSON数据。它将该JSON中的所有URL存储在ArrayList中。我创建了一个AsyncTask来处理这个问题。当我运行该程序时,我收到以下错误:
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:299)
at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
Caused by: java.lang.NullPointerException
at com.example.bab.googleimagesapi.MainActivity$myTask.doInBackground(MainActivity.java:84)
at com.example.bab.googleimagesapi.MainActivity$myTask.doInBackground(MainActivity.java:58)
at android.os.AsyncTask$2.call(AsyncTask.java:287)
at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:137)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
at java.lang.Thread.run(Thread.java:856)
它表示第58和84行有一个NullPointerException
。
这是我的Asynctask的代码,我已经注释了第58和84行,所以你可以找到它们。
private class myTask extends AsyncTask<Void, Void, ArrayList<String> > { **//LINE 58**
@Override
protected ArrayList<String> doInBackground(Void... params) {
String imageUrl = "x";
String query = editText.getText().toString(); //get users search query
query = query.replaceAll(" ", "%20");
try {
URL url = new URL("https://ajax.googleapis.com/ajax/services/search/imagesv=1.0&q=" + query + "&userip=INSERT-USER-IP");
URLConnection connection = url.openConnection();
connection.addRequestProperty("referer", "www.github.com");
String line;
StringBuilder builder = new StringBuilder();
BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()));
while ((line = reader.readLine()) != null) {
builder.append(line);
}
try {
JSONObject jsonObject = new JSONObject(builder.toString());
JSONObject sub = jsonObject.getJSONObject("responseData");
JSONArray results = sub.getJSONArray("results");
for(int x = 0; x < results.length(); x++) {
JSONObject jsonImage = results.getJSONObject(x);
imageUrl = jsonImage.getString("url");
urlList.add(imageUrl); **//LINE 84**
}
return urlList;
} catch (JSONException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
// textResult = e.toString();
} catch (IOException e) {
e.printStackTrace();
//textResult = e.toString();
}
return null;
}
protected void onPostExecute(ArrayList<String> list) {
for(int x = 0; x < list.size(); x++){
textView.append(list.get(x));
}
}
}
我是初次使用AsyncTask和Android的新手,所以我感谢任何帮助!
答案 0 :(得分:2)
您需要在使用之前初始化urlList
:
urlList = new ArrayList<>();
答案 1 :(得分:1)
您需要在urlList
myTask
urlList = new ArrayList<>();
或宣布为最终版。
另外,我不建议您在Android上使用AsyncTask
。在这里阅读更多相关信息:
http://simonvt.net/2014/04/17/asynctask-is-bad-and-you-should-feel-bad/