English 中文(简体)
Geocoder 出错。 get FromLocationName 方法
原标题:Error with Geocoder.getFromLocationName method

我试图使用地理编码,通过街道地址获得某个地方的纬度/纬度。这是我的代码:

 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. 我不知道它为什么出现。请帮助。

最佳回答

请检查“地址列表”是否无效, 如果不是无效的话, 请继续如下 。

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

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签