2013-07-17 155 views
0

我從Google Play開發者控制檯收到以下錯誤消息。 除了try/catch,我找不到任何可以給出空指針的東西,它是在API 8中不可用的 Geocoder.isPresent()嗎?Geocoder.isPresent()會導致NullPointerException?

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

如果你看到的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或以上。希望這可以幫助。

+0

好的謝謝Shobhit,我將調整minSDK並將代碼更改爲:if(Geocoder.isPresent()!= null&Geocoder.isPresent()) –

+0

Yaa,請嘗試更改minsdk並執行if(Geocoder.isPresent ()!= null && Geocoder.isPresent())'。注意'&&'不要'&'。 –

+0

謝謝,我們同時提出了這個問題..... –

相關問題