我有一个应用程序应该通过按下按钮从纬度和经度中找到地址名称。我一直在使用反向地理编码,直到几天我才尝试添加其他按钮。从那以后,每次调试代码时,它都会跳过地址行并退出方法。我无法弄清楚代码在以前工作正常时出了什么问题。
这是我一直在使用的代码
public class MainActivity extends Activity implements LocationListener{
protected LocationManager locationManager;
protected LocationListener locationListener;
protected Context context;
//declaration of variables for latitude and longitude
TextView txtLat;
TextView txtLon;
String lat;
String provider;
protected String latitude,longitude;
protected boolean gps_enabled, network_enabled;
private static final long time_bw_up_min = 50*60;
double latitudedouble;
double longitudedouble;
List<AddressList> addressList;
private static final long dist_change_update_min = 10;
public MainActivity() {
this.addressList = null;
}
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//initialize the text displays showing latitude and longitude
txtLat = (TextView) findViewById(R.id.latitude);
txtLon = (TextView) findViewById(R.id.longitude);
//run the method to access the devices location
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,time_bw_up_min,dist_change_update_min, this);
}
public void goButtonClicked(View v) {
Geocoder geocoder = new Geocoder(this, Locale.ENGLISH);
String result = null;
try {
List<Address> list = geocoder.getFromLocation(latitudedouble, longitudedouble, 1);
if (list != null && list.size()>0) {
Address address = list.get(0);
result = address.getAddressLine(0) + ", " + address.getLocality();
txtLat.setText(result);
}
} catch (IOException ex) {
Logger.getLogger(MainActivity.class.getName()).log(Level.SEVERE, null, ex);
}
}
public void onLocationChanged(Location location) {
//find the text view for latitude
txtLat = (TextView) findViewById(R.id.latitude);
//change the text from the text view to the latitude of the device
txtLat.setText("Latitude: " + location.getLatitude());
//declare a variable to save the device's latitude
latitudedouble = location.getLatitude();
//find the text view for longitude
txtLon = (TextView) findViewById(R.id.longitude);
//change the text from the text view to the longitude of the device
txtLon.setText("Longitude: " + location.getLongitude());
//declare a variable to save the device's longitude
longitudedouble = location.getLongitude();
}
@Override
public void onProviderDisabled(String provider) {
Log.d("Latitude","disable");
}
@Override
public void onProviderEnabled(String provider) {
Log.d("Latitude","enable");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d("Latitude","status");
}
}
}
其他方法运行正常。它只是goButtonClicked
方法下没有执行的代码