GeoCoder 将值之一返回为 null

GeoCoder is returning one of value as null

我正在尝试获取国家、州、城市、Pincode、子区域的详细信息,Address.But 有时 returns 城市为 null 或子区域为 null.Heres 附加图像

这是我的代码..

 String locationCountry = "", locationState = "", locationCity = "", locationPincode = "", locationSubRegion = "", locationAddress = "";
    Geocoder gCoder;
    List<Address> addresses;
    double latitude = 0, longitude = 0;


    GPStracker g = new GPStracker(getApplicationContext());
            Location l = g.getLocation();


            if (l != null) {
                latitude = l.getLatitude();
                longitude = l.getLongitude();

            }else {
                 l = g.getLocation();
            }

            gCoder = new Geocoder(this, Locale.getDefault());

            try {
                addresses = gCoder.getFromLocation(latitude, longitude, 1);
                if (addresses != null && addresses.size() > 0) {

                    locationCountry = addresses.get(0).getCountryName();
                    locationState = addresses.get(0).getAdminArea();
                    locationCity = addresses.get(0).getLocality();
                    locationPincode = addresses.get(0).getPostalCode();
                    locationSubRegion = addresses.get(0).getSubLocality();
                    locationAddress = addresses.get(0).getAddressLine(0);

                }

            } catch (Exception e) {
            }

地理编码器 API 将 return 值取决于经纬度,如果 API 数据库没有这些值(城市则它将 return NULL只是,据我所知,您对此无能为力。您必须优雅地处理这些情况,以免您的应用程序崩溃。

处理这些情况包括你必须使用 if 检查条件是特定值是否为 null 或者它有一些值,因为如果你在代码中的某处使用任何值而不检查,如果value 为 Null 那么当该特定值变为 null 时应用程序可能会崩溃。