【发布时间】:2012-07-09 23:16:35
【问题描述】:
我有一个方法 searchPlace() 使用谷歌地图更新 A 类 (FindItOnMap) 中的自定义 Place Object 的 static Arrays,以及更新各种地理点的方法 updateMap()。
我调用这些方法Button.onClick 并且一切正常。
由于这些方法使用互联网数据,此操作可能需要一段时间,我一直在寻找扩展 AsyncTask 以在处理过程中显示等待对话框的 A 类内部的内部类 B(YourCustomAsyncTask) 的实现这两种方法中的一种
一位用户以这种形式提出了解决方案(显然似乎有效):
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 项目...
这怎么可能?
如果我在内部类之外调用方法 search(location, name) 一切正常,updateMapWithResult(); 更新所有地理点,所以这两种方法都可以。仅当我尝试在内部类中调用它时,json 调用似乎正在工作,但静态变量 foundResults 填充有 null 元素并且程序无法正常工作。
有什么建议吗?
【问题讨论】:
-
foundResults是static是否有特定原因?您的内部类应该能够在没有static的情况下访问它...不确定这是否会解决您的问题,但值得一试。 -
是的,是静态的,因为在类内的各种其他静态过程中使用
标签: android static android-asynctask inner-classes