Geocoder.getFromLocationName方法出错

时间:2012-05-22 16:08:10

标签: java android google-maps latitude-longitude google-geocoder

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

 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。我不知道它为什么会产生。请帮忙。

1 个答案:

答案 0 :(得分:2)

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

 if (addressList == null)
    {
             Log.d(TAG,"Latitude and longitude not found");
    }
     else {
     Address location = addressList.get(0);
    latitude = location.getLatitude();
     longitude = location.getLongitude();
     }