简体   繁体   中英

Geocoder.isPresent() causes NullPointerException?

I got the following error message from Google Play Developer Console. Besides the try/catch I cannot find anything that could give a null pointer, was it the Geocoder.isPresent() which is not available in API 8?

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxxxxxx.SearchActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
at android.app.ActivityThread.access$600(ActivityThread.java:140)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4898)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.xxxxxxxx.SearchActivity.updateUserLocationNameBasedOnNewCoordinates(SearchActivity.java:367)
at com.xxxxxxxx.SearchActivity.useDeviceLocation(SearchActivity.java:338)
at com.xxxxxxxx.SearchActivity.start(SearchActivity.java:495)
at com.xxxxxxxx.SearchActivity.onCreate(SearchActivity.java:253)
at android.app.Activity.performCreate(Activity.java:5206)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
... 11 more

ACTIVITY:

    public void updateUserLocationNameBasedOnNewCoordinates() {

    String city = null;
    String country = null;

    Geocoder geocoder = new Geocoder(this, Locale.getDefault());
    List<Address> list;

    if (Geocoder.isPresent()) {
        try {
            list = geocoder.getFromLocation(userLocation.getLatitude(),
                    userLocation.getLongitude(), 1);
            Address address = list.get(0);
            city = address.getLocality();
            country = address.getCountryName();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    if (city == null || country == null)
        locationDescription = "Map location";
    else
        locationDescription = city + " " + country;

}

MANIFEST:

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

If you see the documentation of Geocoder.isPresent() , it says: Lack of network connectivity may still cause these methods to return null or empty lists. So make sure you are connected.

Other important thing is that the document says that it was added in API level 9 but you have stated android:minSdkVersion=8 in the manifest. Try to mention android:minSdkVersion=9 or above. Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM