Android AsyncTask ONPostExecute

时间:2012-12-19 07:56:49

标签: android android-asynctask

我在这里有AsyncTask.class我解析json并返回List,并且在postExecute中我需要将此List添加到我的适配器并通知更改,但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)

LoadTask.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
        }

    }
}

1 个答案:

答案 0 :(得分: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