城市名称给我null

时间:2015-11-29 20:11:42

标签: android android-location

我的代码为我的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)

1 个答案:

答案 0 :(得分:0)

根据文档getFromLocation可以返回大小为0的列表,如果没有找到匹配项,或者没有可用的后端服务,则返回null。您的代码需要考虑到这种可能性。