仅使用get()方法后AsyncTask的返回值 [英] AsyncTask return value only after using get() method

查看:118
本文介绍了仅使用get()方法后AsyncTask的返回值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建活动,它应该返回GeoPoint对象的数组后,用户点击该按钮。 code的执行HTTP请求和解析的答案被提取到AsyncTask的。在 onPostExecute()方法我已经分配 overlayList 返回值doInBackground() 方法,但它没有工作,

I've created activity, which should return array of GeoPoint after user clicked the button. Code which perform http request and parse answer is extracted to AsyncTask. In the onPostExecute() method I've assigned overlayList with value returned by doInBackground() method, but it didn't work and

overlayList.size()  

thows一个NullPointerException异常。这是我原来的code:

thows an NullPointerException. Here is my original code:

public class MyActivity extends Activity {

Button bt;
TextView tv1;
List<GeoPoint> overlayList;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    bt = (Button) findViewById(R.id.button);
    tv1 = (TextView) findViewById(R.id.textView);

    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            String query = "http://maps.googleapis.com/maps/api/directions/json?origin=bla-bla&destination=bla-bla&sensor=false";

            Request mat =  new Request();
            mat.execute(query);

            if (overlayList.size() > 0){
                tv1.setText("List is OK!");
            }

        }
    });
}

private class Request extends AsyncTask<String, Void, ArrayList<GeoPoint>> {

    @Override
    protected ArrayList<GeoPoint> doInBackground(String... params) {
        return parse(connect(params[0]));
    }

    @Override
    protected void onPostExecute(ArrayList<GeoPoint> geoPoints) {
        super.onPostExecute(geoPoints);
        overlayList = geoPoints;
    }

    public JSONObject connect(String url) {
        ...    
    }

    public ArrayList<GeoPoint> parse(JSONObject jsonObject) {
        ...
    }

}

但是,如果我将修改我的 OnClickListener 以这样的方式:

HttpRequest mat =  new HttpRequest();
mat.execute(query);

try {
    overlayList = mat.get();
} catch (InterruptedException e) {
    e.printStackTrace();
} catch (ExecutionException e) {
    e.printStackTrace();
}

在列表中的所有去确定, overlayList.size()返回大小。所以,我的问题 - 为什么是 onPostExecute()方法不初始化我的列表

all going ok and overlayList.size() return size of the list. So, my question - why is onPostExecute() method do not initialize my list?

推荐答案

这是的AsyncTask 不正是它的名字所暗示 - 的 doInBackground(.. 。)方法异步运行在一个独立的线程,而code。在的onCreate(...)继续运行。

An AsyncTask does exactly what it's name suggests - the doInBackground(...) method runs asynchronously on a separate thread while the code in onCreate(...) continues to run.

在你的code在这里...

In your code here...

mat.execute(query);

if (overlayList.size() > 0){
    tv1.setText("List is OK!");
}

...的如果检查条件的马上的调用后 mat.execute(查询)。换句话说,你的的AsyncTask 一直没有执行它是一个机会 doInBackground(...)方法。

...the if condition is checked immediately after you call mat.execute(query). In other words, your AsyncTask hasn't had a chance to execute it's doInBackground(...) method.

移动此code ...

Move this code...

if (overlayList.size() > 0){
    tv1.setText("List is OK!");
}

...成的 onPostExecute(...)方法,你的的AsyncTask

编辑:triggs指出,在下面的评论,称的AsyncTask 的get()方法将阻塞主线程和等待结果被返回。这实际上使使用的AsyncTask 成为一个同步操作在这种情况下,有没有点使用的AsyncTask

As triggs points out in the comment below, calling the get() method of AsyncTask will block the main thread and wait for the result to be returned. This effectively makes using an AsyncTask become a synchronous operation in which case there's no point in using an AsyncTask.

我能想到的使用的get()方法的唯一原因是从一个线程比主(UI)线程,虽然我想不出其他很多理由这样做。

The only reason I can think of to use the get() method would be from a thread other than the main (UI) thread although I can't think of many reasons to do that.

这篇关于仅使用get()方法后AsyncTask的返回值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆