I have a string that is being populated by edit text fields when someone enters an address to get the lat and long from the address.
final String specAddressStr = specAddress.getText().toString() + " " + specCity.getText().toString() + "," + " " + specState.getText().toString() + " " + specZip.getText().toString();
I need to use this string in an async task, but when I try and reference it and set the string as a global variable to use it in the tasks, it causes the app to force close. Is there a different way to use a dynamically populated string in an async task that I am missing? Here is the async task code as requested:
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;
}
And here is where I am calling the task:
public void getLatandLong(View v) {
String specAddressStr = specAddress.getText().toString() + " "
+ specCity.getText().toString() + "," + " "
+ specState.getText().toString() + " "
+ specZip.getText().toString();
new processLatandLong().execute(specAddressStr);
}
}
You can pass String to
AsyncTask.execute(Your_String)for accessing Vlue indoInBackgroundas :