当有人输入地址以从地址获取lat和long时,我有一个由编辑文本字段填充的字符串。
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);
}
}
答案 0 :(得分:1)
您可以将字符串传递给AsyncTask.execute(Your_String)
以访问doInBackground
中的值为:
specAddressStr = specAddress.getText().toString() + " " +
specCity.getText().toString() + "," + " " +
specState.getText().toString() + " " +
specZip.getText().toString();
new LongOperation().execute(specAddressStr);
private class LongOperation extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
String stredittext=params[0];
}
// your code....
}