我已经创建了活动,它应该在用户单击按钮后返回 GeoPoint 数组。执行 http 请求和解析答案的代码被提取到 AsyncTask。在onPostExecute()
我分配overlayList
了方法返回的值的doInBackground()
方法中,但它不起作用并且
overlayList.size()
抛出 NullPointerException。这是我的原始代码:
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()
方法不初始化我的列表?