0

我从 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" />
4

1 回答 1

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或以上。希望这可以帮助。

于 2013-07-17T03:41:39.153 回答