这是我的 AsyncTask 类。
class AsyncLoad extends AsyncTask<Void, Void, ArrayList<ListData>> {
int i = 0;
protected ArrayList<ListData>[] onPostExecute(ArrayList<ListData>... result) {
return result;
}
protected ArrayList<ListData> doInBackground(Void... parameter) {
ArrayList<ListData> news = null;
int[] id = { 1, 2, 3, 4, 5 };
String[] title = { "title1", "title2", "title3", "title4", "title5" };
String[] category = { "category1", "category2", "category3",
"category4", "category5" };
int[] image = { R.drawable.pic01, R.drawable.pic02, R.drawable.pic03,
R.drawable.pic04, R.drawable.pic05, R.drawable.pic06 };
for (i = 0; i <= id.length - 1; i++) {
news.add(new ListData(id[i], title[i], category[i], image[i]));
}
return news;
}
}
当我尝试执行此任务时,出现此错误:LOG
02-24 21:44:51.265: E/AndroidRuntime(7684): FATAL EXCEPTION: AsyncTask #1
02-24 21:44:51.265: E/AndroidRuntime(7684): java.lang.RuntimeException: An error occured while executing doInBackground()
02-24 21:44:51.265: E/AndroidRuntime(7684): at android.os.AsyncTask$3.done(AsyncTask.java:278)
02-24 21:44:51.265: E/AndroidRuntime(7684): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-24 21:44:51.265: E/AndroidRuntime(7684): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-24 21:44:51.265: E/AndroidRuntime(7684): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-24 21:44:51.265: E/AndroidRuntime(7684): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-24 21:44:51.265: E/AndroidRuntime(7684): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
02-24 21:44:51.265: E/AndroidRuntime(7684): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
02-24 21:44:51.265: E/AndroidRuntime(7684): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
02-24 21:44:51.265: E/AndroidRuntime(7684): at java.lang.Thread.run(Thread.java:856)
02-24 21:44:51.265: E/AndroidRuntime(7684): Caused by: java.lang.NullPointerException
这是 ListData.java:
package com.example.test2;
public class ListData {
int id;
String title;
String category;
int image;
ListData(int _id, String _title, String _category, int _image) {
id = _id;
title = _title;
category = _category;
image = _image;
}
}
在 MainActivity 我只是执行 AsynTask ( AsyncLoad load = new AsyncLoad(); load.execute(); )
请帮我找出我错在哪里!