誰かが住所を入力して住所から緯度と経度を取得したときに、編集テキストフィールドによって入力される文字列があります。
final String specAddressStr = specAddress.getText().toString() + " " + specCity.getText().toString() + "," + " " + specState.getText().toString() + " " + specZip.getText().toString();
この文字列を非同期タスクで使用する必要がありますが、この文字列を参照して、タスクで使用するために文字列をグローバル変数として設定すると、アプリが強制的に閉じられます。私が見逃している非同期タスクで動的に入力された文字列を使用する別の方法はありますか?要求された非同期タスクコードは次のとおりです。
public class LowSignal extends Activity {
String specAddressStr;
private class processLatandLong extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
List<Address> foundGeocode = null;
// find the addresses by using getFromLocationName() method with the given address
try {
foundGeocode = new Geocoder(LowSignal.this).getFromLocationName(specAddressStr, 1);
foundGeocode.get(0).getLatitude(); // getting latitude
foundGeocode.get(0).getLongitude();// getting longitude
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (foundGeocode !=null) {
returnedLat.setText(String.valueOf(foundGeocode.get(0).getLatitude()));
returnedLong.setText(String.valueOf(foundGeocode.get(0).getLongitude()));
} else {
returnedLat.setText("Unable to find Latitude. Please try again.");
returnedLong.setText("Unable to find Longitude. Please try again.");
}
return null;
}
そして、これが私がタスクと呼んでいるところです:
public void getLatandLong(View v) {
String specAddressStr = specAddress.getText().toString() + " "
+ specCity.getText().toString() + "," + " "
+ specState.getText().toString() + " "
+ specZip.getText().toString();
new processLatandLong().execute(specAddressStr);
}
}