我想使用AsynTask来解析XML数据。 所以在我的活动中:
static List<String> citations = null;
static void setCitations(List<String> data) {
citations = data;
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
new data().execute();
在数据类中我已经:
class data extends AsyncTask<Void, Void, List<String>> {
private XMLReader saxReader;
private SimpleContentHandler simpleContentHandler;
private List<String> citations;
@Override
protected void onPreExecute() {
Log.i("AsyncTask", "onPreExecute");
}
@Override
public void onPostExecute(List<String> citations) {
GuiguiActivity.setCitations(citations);
}
@Override
protected List<String> doInBackground(Void... params) {
citations.add("TEST 1");citations.add("TEST 2");
return citations;
}
}
而不是citations.add我是我的XML解析器,但即使在这种情况下也会出现citations.add错误。
10-24 15:28:46.647: E/AndroidRuntime(1160): FATAL EXCEPTION: AsyncTask #1
10-24 15:28:46.647: E/AndroidRuntime(1160): java.lang.RuntimeException: An error occured while executing doInBackground()
10-24 15:28:46.647: E/AndroidRuntime(1160): at android.os.AsyncTask$3.done(AsyncTask.java:278)
10-24 15:28:46.647: E/AndroidRuntime(1160): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
10-24 15:28:46.647: E/AndroidRuntime(1160): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
10-24 15:28:46.647: E/AndroidRuntime(1160): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
10-24 15:28:46.647: E/AndroidRuntime(1160): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
10-24 15:28:46.647: E/AndroidRuntime(1160): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
10-24 15:28:46.647: E/AndroidRuntime(1160): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
10-24 15:28:46.647: E/AndroidRuntime(1160): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
10-24 15:28:46.647: E/AndroidRuntime(1160): at java.lang.Thread.run(Thread.java:856)
10-24 15:28:46.647: E/AndroidRuntime(1160): Caused by: java.lang.NullPointerException
10-24 15:28:46.647: E/AndroidRuntime(1160): at guigui.first.project.data.doInBackground(data.java:82)
10-24 15:28:46.647: E/AndroidRuntime(1160): at guigui.first.project.data.doInBackground(data.java:1)
10-24 15:28:46.647: E/AndroidRuntime(1160): at android.os.AsyncTask$2.call(AsyncTask.java:264)
10-24 15:28:46.647: E/AndroidRuntime(1160): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
我看不出问题出在哪里。
谢谢:)
答案 0 :(得分:5)
您没有初始化citations
字段。像这样在data
类中添加一个构造函数:
public data(){
citations = new ArrayList<String>();
}
答案 1 :(得分:1)
在AsyncTask中,您需要初始化引用字段,如下所示:
class data extends AsyncTask<Void, Void, List<String>> {
private XMLReader saxReader;
private SimpleContentHandler simpleContentHandler;
//I chose and array list here as a concrete implmentation. But you could use any other
//class that also implements the List interface if you want to.
private List<String> citations = new ArrayList<String>();
@Override
protected void onPreExecute() {
Log.i("AsyncTask", "onPreExecute");
}
@Override
public void onPostExecute(List<String> citations) {
GuiguiActivity.setCitations(citations);
}
@Override
protected List<String> doInBackground(Void... params) {
citations.add("TEST 1");citations.add("TEST 2");
return citations;
}
}
另外两件事: