我试图使用地理编码,通过街道地址获得某个地方的纬度/纬度。这是我的代码:
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. get FromLocationName(Addr,10) 的调用中( 我通过 System. out. pripln 检查过 ; 该行之后的东西不会被打印到日志) 。
这是来自 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.NullPointer Exception. 我不知道它为什么出现。请帮助。