Geocoder.isPresnet()失败,出现NullPointerException

时间:2019-09-16 07:52:22

标签: android nullpointerexception geocoder

在我的应用中,我使用Android的Geocoder。但是,在某些设备(Android 4,5,6和8)上,出现此崩溃:

AT

崩溃似乎发生在Geocoder.isPresent()的实现中,看起来像这样:

Fatal Exception: java.lang.NullPointerException
Attempt to invoke interface method 'boolean android.location.ILocationManager.geocoderIsPresent()' on a null object reference
com.tawkon.data.lib.model.LocationReport.<init>

什么会使ILocationManager为空?

这是我使用Geocoder的方式:

public static boolean isPresent() {
        IBinder b = ServiceManager.getService(Context.LOCATION_SERVICE);
        ILocationManager lm = ILocationManager.Stub.asInterface(b);
        try {
            return lm.geocoderIsPresent();
        } catch (RemoteException e) {
            Log.e(TAG, "isPresent: got RemoteException", e);
            return false;
        }
    }

目前,我唯一想到的“解决方案”是用if(Geocoder.isPresent()) { Geocoder geocoder = new Geocoder(ctx, Locale.ENGLISH); try { List<Address> addresses = geocoder.getFromLocation(this.latitude, this.longitude, 1); if (addresses != null && addresses.size() > 0) { Address fetchedAddresss = addresses.get(0); if (fetchedAddresss.getCountryName() != null) this.country = fetchedAddresss.getCountryName(); if (fetchedAddresss.getAdminArea() != null)// Get State this.state = fetchedAddresss.getAdminArea(); if (fetchedAddresss.getSubAdminArea() != null)// Get County this.county = fetchedAddresss.getSubAdminArea(); if (fetchedAddresss.getLocality() != null)// Get City this.city = fetchedAddresss.getLocality(); if (fetchedAddresss.getPostalCode() != null) this.postalCode = fetchedAddresss.getPostalCode(); } } catch (IOException | IllegalArgumentException e) { e.printStackTrace(); } } 包围整个Geocoder逻辑。还有什么我可以解决的吗?

0 个答案:

没有答案