【发布时间】:2015-11-29 20:11:41
【问题描述】:
我的代码为我的 android 应用程序提供了正确的经度和纬度,但为我尝试调试代码的城市名称提供了 null 我的纬度和经度被传递给我的 try catch 块,但它无法运行“if”语句。
private class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
editLocation.setText("");
pb.setVisibility(View.INVISIBLE);
Toast.makeText(getBaseContext(), "Location changed : Lat: " +
loc.getLatitude() + " Lng: " + loc.getLongitude(),
Toast.LENGTH_SHORT).show();
String longitude = "Longitude: " + loc.getLongitude();
Log.v(TAG, longitude);
String latitude = "Latitude: " + loc.getLatitude();
Log.v(TAG, latitude);
String cityName = null;
Geocoder gcd = new Geocoder(getBaseContext(), Locale.getDefault());
List<Address> addresses;
try {
addresses = gcd.getFromLocation(loc.getLatitude(), loc
.getLongitude(), 1);
if (addresses.size() > 0) {
System.out.println(addresses.get(0).getLocality());
}
cityName = addresses.get(0).getLocality();
Log.v(TAG, cityName);
} catch (IOException e) {
e.printStackTrace();
}
String s = longitude + "\n" + latitude +
"\n\nMy Currrent City is: " + cityName;
editLocation.setText(s);
}
logcat 错误
11-29 15:35:45.675 8206-8206/com.example.finalprojectv1 W/System.err: java.io.IOException: Timed out waiting for response from server
11-29 15:35:45.675 8206-8206/com.example.finalprojectv1 W/System.err: at android.location.Geocoder.getFromLocation(Geocoder.java:151)
11-29 15:35:45.675 8206-8206/com.example.finalprojectv1 W/System.err: at com.example.finalprojectv1.WeatherGPS$MyLocationListener.onLocationChanged(WeatherGPS.java:138)
11-29 15:35:45.675 8206-8206/com.example.finalprojectv1 W/System.err: at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:281)
11-29 15:35:45.675 8206-8206/com.example.finalprojectv1 W/System.err: at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:210)
11-29 15:35:45.675 8206-8206/com.example.finalprojectv1 W/System.err: at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:226)
11-29 15:35:45.675 8206-8206/com.example.finalprojectv1 W/System.err: at android.os.Handler.dispatchMessage(Handler.java:102)
11-29 15:35:45.675 8206-8206/com.example.finalprojectv1 W/System.err: at android.os.Looper.loop(Looper.java:155)
11-29 15:35:45.675 8206-8206/com.example.finalprojectv1 W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5696)
11-29 15:35:45.675 8206-8206/com.example.finalprojectv1 W/System.err: at java.lang.reflect.Method.invoke(Native Method)
11-29 15:35:45.675 8206-8206/com.example.finalprojectv1 W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
11-29 15:35:45.675 8206-8206/com.example.finalprojectv1 W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
11-29 15:35:45.675 8206-8206/com.example.finalprojectv1 W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
【问题讨论】: