Geocoder.isPresent()导致NullPointerException?

时间:2013-07-17 03:29:05

标签: java android google-geocoder

我从Google Play开发者控制台收到以下错误消息。 除了try / catch我找不到任何可以给出空指针的东西,是吗? API 8中没有Geocoder.isPresent()?

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

活动:

    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;

}

清单:

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

1 个答案:

答案 0 :(得分:1)

如果您看到Geocoder.isPresent()的文档,则会说:Lack of network connectivity may still cause these methods to return null or empty lists.请确保您已连接。

其他重要的是该文档说它是在API级别9中添加的,但您已在清单中声明android:minSdkVersion=8。尽量提及android:minSdkVersion=9或以上。希望这可以帮助。