0

我在这里有 AsyncTask.class 我解析 json 并返回列表,在 postExecute 中我需要将此列表添加到我的适配器并通知更改,但是 LogCat 给出错误,我该如何解决这个问题?

我的适配器有公共状态,为什么我不能添加到他的结果?

 FATAL EXCEPTION: main
 java.lang.NullPointerException
    at com.gazetaimage.LoadTask.onPostExecute(LoadTask.java:84)
    at com.gazetaimage.LoadTask.onPostExecute(LoadTask.java:1)
    at android.os.AsyncTask.finish(AsyncTask.java:631)
    at android.os.AsyncTask.access$600(AsyncTask.java:177)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5191)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
    at dalvik.system.NativeStart.main(Native Method)

加载任务.java

public class LoadTask extends AsyncTask<Void, Void, List<String>> {

    MainActivity main = new MainActivity();

    private int page = 1;

    public int addPage(int i){
        return page = i;
    }

    @Override
    protected List<String> doInBackground(Void... params) {

        InputStream ips = null; 
        JSONObject jsonObj = null;
        String json = "";       
        try {
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpResponse response = httpClient.execute(new HttpPost("http://iapp.gazeta.uz/fotolenta?page="+page)); 
            ips = response.getEntity().getContent();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader bufff = new BufferedReader(new InputStreamReader(ips, "UTF-8"));
            StringBuilder sb = new StringBuilder();
            String line = null;

            while ((line = bufff.readLine()) != null){
                sb.append(line + "\n");                 
            }
            ips.close();
            json = sb.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            jsonObj = new JSONObject(json);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        if(jsonObj != null){

        JSONArray array = jsonObj.optJSONArray("img_list"); 
            for (int i = 0; i < array.length(); i++) {
                main.thumbs.add(array.optJSONObject(i).optString("thumbnail"));
            }
        }
        else{
            Log.d("Log", "Nullll");
        }

        return main.thumbs;
    }

    @Override
    protected void onPostExecute(List<String> result) {
        if (result != null){
            main.adapter.add(result);// HERE I HAVE Error 84
        }

    }
}
4

1 回答 1

2

您不能在任务内创建活动的实例,并期望它将是相同的活动正在运行..

相反,您可以创建interface,将其传递给AsyncTask(在构造函数中),然后在onPostExecute

例如:

你的界面:

public interface OnTaskCompleted{
    void onTaskCompleted(values);
}

您的活动:

public YourActivity implements OnTaskCompleted{
    //your Activity
    YourTask  task = new YourTask(this); // here is the initalization code for your asyncTask
}

还有你的 AsyncTask:

public YourTask extends AsyncTask<Object,Object,Object>{ //change Object to required type
    private OnTaskCompleted listener;

    public YourTask(OnTaskCompleted listener){
        this.listener=listener;
    }

    //required methods

    protected void onPostExecute(Object o){
        //your stuff
        listener.onTaskCompleted(values);
    }
}

取自https://stackoverflow.com/a/9963705/1434631

于 2012-12-19T07:59:07.107 回答