0

这是我的 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(); )

请帮我找出我错在哪里!

4

2 回答 2

3

嗯,是。看这个:

ArrayList<ListData> news = null;

接着:

news.add(new ListData(id[i], title[i], category[i], image[i]));

你期望它如何工作?news永远不会有非空值,所以当你打电话时news.add得到一个NullPointerException. 你想要这样的东西:

List<ListData> news = new ArrayList<ListData>();

(注意我是如何用作ArrayList具体实现的,但我更喜欢对接口进行一般编程,我将news变量本身设置为 type List<ListData>。)

于 2013-02-24T19:58:38.037 回答
1

你从来没有实例化你的 list news。你需要这样做:

ArrayList<ListData> news = new ArrayList<ListData>();
于 2013-02-24T19:58:54.260 回答