我不知道我的代码有什么问题。它应该返回一个视频标题列表,但强制关闭。我做错了什么?
代码(请原谅基本评论)。
package com.aer.illbehonest;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import org.apache.commons.io.IOUtils;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class VideosList extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videos_list);
new GetTitlesTask().execute("");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_videos_list, menu);
return true;
}
class GetTitlesTask extends AsyncTask<String, Void, ArrayList<String>> {
ArrayList<String> titlesList;
@Override
protected ArrayList<String> doInBackground(String... args) {
URL jsonURL = null;
try {
jsonURL = new URL("http://gdata.youtube.com/feeds/api/users/illbehonest/uploads?v=2&alt=jsonc");
} catch (MalformedURLException e) {
System.out.println("Malformed URL... check");
// TODO Auto-generated catch block
e.printStackTrace();
}
URLConnection jc = null;
try {
jc = jsonURL.openConnection();
} catch (IOException e) {
System.out.println("jc problem");
// TODO Auto-generated catch block
e.printStackTrace();
}
InputStream is = null;
try {
is = jc.getInputStream();
} catch (IOException e) {
System.out.println("is problem");
// TODO Auto-generated catch block
e.printStackTrace();
}
String jsonTxt = null;
try {
jsonTxt = IOUtils.toString(is);
} catch (IOException e) {
System.out.println("jsontxt problem");
// TODO Auto-generated catch block
e.printStackTrace();
}
JsonObject json = new JsonParser().parse(jsonTxt).getAsJsonObject();
JsonObject jdata = json.getAsJsonObject("data");
JsonArray jentry = jdata.getAsJsonArray("items");
for (int entryNumber = 0; entryNumber<=25; entryNumber++){
JsonObject entry = jentry.get(entryNumber).getAsJsonObject();
titlesList.add(entry.get("title").toString());
}
return titlesList;
}
protected void onPostExecute(ArrayList<String> result) {
ListView listView = (ListView)VideosList.this.findViewById(R.id.videolist);
ArrayAdapter<String> adapter = new ArrayAdapter<String> (VideosList.this,
android.R.layout.simple_list_item_1, android.R.id.text1, titlesList);
(listView).setAdapter(adapter);
}
}
}
这是我的LogCat输出(注意我正在使用远程物理设备)。
01-21 01:19:11.430: W/dalvikvm(389): threadid=11: thread exiting with uncaught exception (group=0x41b28930)
01-21 01:19:11.696: E/AndroidRuntime(389): FATAL EXCEPTION: AsyncTask #1
01-21 01:19:11.696: E/AndroidRuntime(389): java.lang.RuntimeException: An error occured while executing doInBackground()
01-21 01:19:11.696: E/AndroidRuntime(389): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-21 01:19:11.696: E/AndroidRuntime(389): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
01-21 01:19:11.696: E/AndroidRuntime(389): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
01-21 01:19:11.696: E/AndroidRuntime(389): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
01-21 01:19:11.696: E/AndroidRuntime(389): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-21 01:19:11.696: E/AndroidRuntime(389): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-21 01:19:11.696: E/AndroidRuntime(389): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-21 01:19:11.696: E/AndroidRuntime(389): at java.lang.Thread.run(Thread.java:856)
01-21 01:19:11.696: E/AndroidRuntime(389): Caused by: java.lang.NullPointerException
01-21 01:19:11.696: E/AndroidRuntime(389): at com.aer.illbehonest.VideosList$GetTitlesTask.doInBackground(VideosList.java:87)
01-21 01:19:11.696: E/AndroidRuntime(389): at com.aer.illbehonest.VideosList$GetTitlesTask.doInBackground(VideosList.java:1)
01-21 01:19:11.696: E/AndroidRuntime(389): at android.os.AsyncTask$2.call(AsyncTask.java:287)
01-21 01:19:11.696: E/AndroidRuntime(389): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
01-21 01:19:11.696: E/AndroidRuntime(389): ... 4 more
答案 0 :(得分:0)
从entryNumber&lt; = 25更改for循环增量; to entryNumber&lt; 25;可能会解决它。
在您从服务器收到的JSON数据中,只有25个项目从0到24,您尝试访问第26项。
您可能在for循环中使用了entryNumber&lt; = jentry.Length。