6

エミュレータと携帯電話 (Android 4.1 の Nexus S) の両方で Geocoder.getLocationFromName() メソッドを使用していますが、次の例外が発生します。

java.io.IOException: Service not Available

これがエミュレーターに表示されることについて多くの質問があり ( example )、それらのほとんどは、問題があるのはエミュレーターの特定のバージョンであると言います。ただし、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);

そして、これは私のマニフェストです:

<?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>
4

1 に答える 1

7

アプリケーションのビルド ターゲットが、単にAndroid 4.0 (または類似のもの)ではなく、 Google APIのいずれかに設定されていることを確認してください。Eclipse でこの設定を変更するには、プロジェクトを右クリックし、[プロパティ]、[Android] の順に選択します。または、次のようにファイルを編集することもできます。project.properties

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

その理由は、Geocoderクラスが Android のコア フレームワークに存在するが、適切に機能するために Google API によって提供されたコードに依存しているためです。AVD に Google API が含まれている場合でも、プロジェクトはその特定のビルド ターゲットに対してビルドする必要があります。

于 2012-09-23T20:06:17.293 に答える