Android Geocoder.getLocationFromName()抛出IOException:服务在Device上不可用

Mar*_*kis 6 android geocoding geolocation

我在模拟器和我的手机(带有Android 4.1的Nexus S)上使用Geocoder.getLocationFromName()方法,我得到以下异常:

java.io.IOException: Service not Available
Run Code Online (Sandbox Code Playgroud)

关于这个问题有很多问题出现在模拟器上(示例),并且大多数人都说这是模拟器的特定版本存在问题.但是,我的AVD(2.3和4.1)和我的手机都出现了异常.

我的手机和AVD都有互联网连接.我的API版本是16(android 4.1),但我也尝试过较旧版本.两种AVD都包含Google API.

有什么想法在这里发生了什么?

这是相关的代码段:

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

这是我的表现:

<?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>
Run Code Online (Sandbox Code Playgroud)

twa*_*ton 7

确保您的应用程序的构建目标设置为其中一个Google API,而不仅仅是Android 4.0(或类似版本).您可以通过右键单击项目并选择"属性"然后选择"Android"来更改Eclipse中的此设置.或者,您可以简单地将project.properties文件编辑为以下内容:

# Project target
target=Google Inc.:Google APIs:16
Run Code Online (Sandbox Code Playgroud)

原因是Geocoder该类存在于核心Android框架中,但依赖于Google API提供的代码才能正常运行.即使您的AVD包含Google API,您的项目仍需要针对该特定构建目标构建.

  • 我到处寻找答案,最后这很简单.感谢Markos Fragkakis提出的问题并感谢twaddington解决了我的问题. (2认同)