Android的地理coder.getLocationFromName()抛出IOException异常:服务不能在设备可用能在、抛出、异常、地理

2023-09-06 04:35:24 作者:颓废

我使用的同时在仿真器和我的手机(Nexus S的与Android 4.1)的地理coder.getLocationFromName()方法,我也得到了以下异常:

  java.io.IOException异常:服务不可用
 

有大量有关此出现在模拟器上的问题(example),大多数人说,这是有问题的仿真器的特定版本。然而,除了在我的AVDS(2.3和4.1)和我的手机会出现两种。

我的两个手机和AVDS有互联网连接。我的API的版本是16(安卓4.1),但我也试着用旧的。这两个AVDS包括谷歌的API。

任何想法是怎么回事?

这是有关code片断:

 地理codeR myGeo codeR =新的地缘codeR(getApplicationContext(),Locale.getDefault());
名单<地址> newAddresses = myGeo coder.getFromLocationName(arg0.toString(),10);
 
微信在Github开源了Hardcoder,对Android开发者有什么影响

这是我的清单:

 < XML版本=1.0编码=UTF-8&GT?;
<舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    包=com.asdasd安卓版code =6安卓VERSIONNAME =1.3.1>
    <使用-SDK安卓的minSdkVersion =16/>
    <使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
    <使用-权限的Andr​​oid:名称=android.permission.ACCESS_NETWORK_STATE/>
    <使用-权限的Andr​​oid:名称=android.permission.ACCESS_COARSE_LOCATION/>
    <应用机器人:图标=@可绘制/ ic_launcher_asdasd
        机器人:标签=@字符串/ APP_NAME>
        <活动机器人:AsdasdActivity名称=机器人:标签=@字符串/ APP_NAME
            机器人:screenOrientation =画像机器人:windowSoftInputMode =stateHidden>
            <意向滤光器>
                <作用机器人:名称=android.intent.action.MAIN/>
                <类机器人:名称=android.intent.category.LAUNCHER/>
            &所述; /意图滤光器>
        < /活性GT;
        <活动机器人:名称=com.google.ads.AdActivity
            机器人:configChanges =键盘| keyboardHidden |定位/>
    < /用途>
< /舱单>
 

解决方案

请确保您的应用程序的构建目标设置为谷歌API之一的,而不是简单的的Andr​​oid 4.0 的(或类似)。您可以通过右键点击你的项目并选择属性,然后在Eclipse中更改此设置的Android。或者,你可以简单地编辑 project.properties 文件类似如下:

 #项目目标
目标=谷歌公司:谷歌的API:16
 

原因是,地理codeR 类是present核心Android框架,而是依赖于code贡献了谷歌的API才能正常工作。即使你的AVD包括谷歌的API,你的项目仍然需要对具体的构建目标建造的。

I am using the Geocoder.getLocationFromName() method on both the emulator and my phone (Nexus S with Android 4.1) and I get the following exception:

java.io.IOException: Service not Available

There are a number of questions about this appearing on the emulator (example), and most of them say that it is a specific version of the emulator that has the problem. However, the exception appears both on my AVDs (2.3 and 4.1) and my phone.

Both my phone and the AVDs have internet connection. My API version is 16 (android 4.1), but I also tried with older ones. Both AVDs include Google APIs.

Any ideas what is going on here?

This is the relevant code snippet:

Geocoder myGeocoder = new Geocoder(getApplicationContext(), Locale.getDefault());
List<Address> newAddresses = myGeocoder.getFromLocationName(arg0.toString(), 10);

And this is my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.asdasd" android:versionCode="6" android:versionName="1.3.1">
    <uses-sdk android:minSdkVersion="16" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <application android:icon="@drawable/ic_launcher_asdasd"
        android:label="@string/app_name">
        <activity android:name=".AsdasdActivity" android:label="@string/app_name"
            android:screenOrientation="portrait" android:windowSoftInputMode="stateHidden">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.google.ads.AdActivity"
            android:configChanges="keyboard|keyboardHidden|orientation" />
    </application>
</manifest>

解决方案

Make sure your application's build target is set to one of the Google APIs and not simply Android 4.0 (or similar). You can change this setting in Eclipse by right-clicking on your project and selecting "Properties" then "Android." Alternatively, you can simply edit the project.properties file to something like the following:

# Project target
target=Google Inc.:Google APIs:16

The reason is that the Geocoder class is present in the core Android framework, but depends on code contributed by the Google APIs to function properly. Even if your AVD includes the Google APIs, your project still needs to be built against that specific build target.

 
精彩推荐
图片推荐