我有一个方法searchPlace()
更新在谷歌地图的类A(FindItOnMap)中的自定义地点对象的static Arrays
和更新各种地址的方法updateMap()
。 我调用这些方法Button.onClick
和所有正常工作。AsyncTask作为内部类和静态字段问题
由于这些方法使用互联网数据,这个操作可能需要一段时间,我一直在寻找类A内的内部类B(YourCustomAsyncTask
)的实现,它扩展了AsyncTask
以在处理这两个过程中显示等待对话方法
的用户建议这种形式的解决方案(即表面上似乎有效):
public class FindItOnMap extends MapActivity {
static Place[] foundResults;
private ProgressDialog dialog;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ricerca_condominio);
mapView = (MapView)findViewById(R.id.mapView);
...........
((ImageButton) findViewById(R.id.btSearch)).setOnClickListener(mSearchListenerListener);
}
OnClickListener mSearchListener = new OnClickListener() {
public void onClick(View v) {
String location=editorLocation.getText().toString();
String name=editorName.getText().toString();
//Call the AsyncTask here
new YourCustomAsyncTask().execute(new String[] {name, location});
}
};
private class YourCustomAsyncTask extends AsyncTask <String, Void, Void> {
@Override
protected void onPreExecute() {
dialog = new ProgressDialog(Main.this);
dialog.setMessage("Loading....");
dialog.setIndeterminate(true);
dialog.setCancelable(true);
dialog.show(); //Maybe you should call it in ruinOnUIThread in doInBackGround as suggested from a previous answer
}
@Override
protected Void doInBackground(String... strings) {
try {
search(strings[0], string[1]);
return null;
} catch(Exception e) {
}
}
@Override
protected void onPostExecute(Void params) {
updateMapWithResult();
dialog.dismiss();
//result
}
.....
}
等待的对话框显示和方法在后台调用, 但是,对于一些奇怪的原因静态列表foundResults
结果填充各种null
项目...
这怎么可能?
如果我调用内部类之外的方法搜索(位置,名称)都可以正常工作,并且updateMapWithResult();
更新所有的地址,所以这两个方法都可以。只有当我试图在内部类中调用这个json调用似乎正在工作,但静态变量foundResults
填充了null
元素和程序无法正常工作。
有什么建议吗?
是否有'foundResults'为'static'的具体原因?你的内部类应该能够访问它,而不是'静态'...不知道这是否能解决你的问题,但值得一试。 – 2012-07-09 23:22:58
是的,是静态的,因为在其他各种静态程序中使用 – Asgard 2012-07-09 23:25:31