我有一個方法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