【发布时间】:2013-03-16 04:54:01
【问题描述】:
我正在尝试使用 htc 愿望 c 反向地理编码,在运行应用程序时,它的抛出服务不可用异常。但是 GeoCoder.isPresent() 正在返回“真”。请帮我找出问题。
这是我的代码:
public class MainActivity extends Activity {
LocationManager lManager;
String provider;
Location location ;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
provider = lManager.getBestProvider(criteria, false);
if(provider!=null && !provider.equals("")){
// Get the location from the given provider
location = lManager.getLastKnownLocation(provider);
}
boolean geoCoder = false;
Geocoder geo = new Geocoder(this, Locale.getDefault());
geoCoder = Geocoder.isPresent();
System.out.println("GEO CODER : "+geoCoder);
try {
List<Address> address = geo.getFromLocation(location.getLatitude(), location.getLongitude(), 3);
System.out.println("Size------------- "+address.size());
/*Address addr = address.get(0);
System.out.println("City "+addr.getLocality());*/
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("in here--------------");
e.printStackTrace();
}
}
日志:
03-16 10:33:58.609: I/System.out(30398): GEO CODER : true
03-16 10:33:58.609: I/System.out(30398): in here--------------
03-16 10:33:58.609: W/System.err(30398): java.io.IOException: Service not Available
03-16 10:33:58.619: W/System.err(30398): at android.location.Geocoder.getFromLocation(Geocoder.java:136)
03-16 10:33:58.619: W/System.err(30398): at com.example.geocoder.MainActivity.onCreate(MainActivity.java:46)
03-16 10:33:58.619: W/System.err(30398): at android.app.Activity.performCreate(Activity.java:4538)
03-16 10:33:58.629: W/System.err(30398): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
03-16 10:33:58.629: W/System.err(30398): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
03-16 10:33:58.629: W/System.err(30398): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
03-16 10:33:58.629: W/System.err(30398): at android.app.ActivityThread.access$600(ActivityThread.java:139)
03-16 10:33:58.629: W/System.err(30398): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
03-16 10:33:58.629: W/System.err(30398): at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 10:33:58.629: W/System.err(30398): at android.os.Looper.loop(Looper.java:156)
03-16 10:33:58.629: W/System.err(30398): at android.app.ActivityThread.main(ActivityThread.java:4987)
03-16 10:33:58.639: W/System.err(30398): at java.lang.reflect.Method.invokeNative(Native Method)
03-16 10:33:58.639: W/System.err(30398): at java.lang.reflect.Method.invoke(Method.java:511)
03-16 10:33:58.639: W/System.err(30398): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-16 10:33:58.639: W/System.err(30398): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-16 10:33:58.649: W/System.err(30398): at dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
-
质量很差:您使用的是什么语言?你的代码在哪里?你试过什么?
-
@AnilM,您在 AndroidManifest.xml 中授予 Internet 权限了吗?
-
是的...我已在我的 AndroidManifest.xml 中授予 Internet 和 Access Fine Location 权限
-
请使用 Stacktrace logcat