0

我正在尝试使用地理编码通过传递街道地址来获取给定地点的纬度/经度。这是我的代码:

 String addr = "Shop No. 2, Payal Cinema, Old Delhi Road, Sec-14, Gurgaon";                             
 Geocoder geocoder= new Geocoder(this, Locale.getDefault());
 List<Address> addressList = null;
 try {

          addressList = geocoder.getFromLocationName(addr, 10);

     } catch (IOException e) {}
 Address address = addressList.get(0);
 if(address.hasLatitude() && address.hasLongitude()){
 lat = address.getLatitude();
 lon = address.getLongitude();
 }                   

错误出现在对 gecoder.getFromLocationName(addr,10) 的调用中(我通过 System.out.println 进行了检查;在该行之后没有将内容打印到 logcat)

这是来自 LogCat 的堆栈跟踪:

 W/dalvikvm(630): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
 E/AndroidRuntime(630): FATAL EXCEPTION: main
 E/AndroidRuntime(630): java.lang.RuntimeException: Unable to start activity ComponentInfo{apt.kangkan/apt.kangkan.AddrMarkrsonMapActivity}: java.lang.NullPointerException
 E/AndroidRuntime(630):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
 E/AndroidRuntime(630):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
 E/AndroidRuntime(630):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
 E/AndroidRuntime(630):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
 E/AndroidRuntime(630):     at android.os.Handler.dispatchMessage(Handler.java:99)
 E/AndroidRuntime(630):     at android.os.Looper.loop(Looper.java:137)
 E/AndroidRuntime(630):     at android.app.ActivityThread.main(ActivityThread.java:4424)
 E/AndroidRuntime(630):     at java.lang.reflect.Method.invokeNative(Native Method)
 E/AndroidRuntime(630):     at java.lang.reflect.Method.invoke(Method.java:511)
 E/AndroidRuntime(630):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
 E/AndroidRuntime(630):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
 E/AndroidRuntime(630):     at dalvik.system.NativeStart.main(Native Method)
 E/AndroidRuntime(630): Caused by: java.lang.NullPointerException
 E/AndroidRuntime(630):     at apt.kangkan.AddrMarkrsonMapActivity.onCreate(AddrMarkrsonMapActivity.java:76)
 E/AndroidRuntime(630):     at android.app.Activity.performCreate(Activity.java:4465)
 E/AndroidRuntime(630):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
 E/AndroidRuntime(630):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
 E/AndroidRuntime(630):     ... 11 more
 I/dalvikvm(630): threadid=3: reacting to signal 3
 I/dalvikvm(630): Wrote stack traces to '/data/anr/traces.txt'

你可以看到有一个 java.lang.NullPointerException。我不知道它为什么会出现。请帮忙。

4

1 回答 1

2

实际上找不到您的地址的纬度和经度,请检查“addressList”是否为空,如果它不为空,则继续如下..

 if (addressList == null)
    {
             Log.d(TAG,"Latitude and longitude not found");
    }
     else {
     Address location = addressList.get(0);
    latitude = location.getLatitude();
     longitude = location.getLongitude();
     }
于 2012-05-22T16:24:10.647 回答