使用自动完成功能 Google 地方 API 给出错误

Using Autocomplete functionality with Google place API giving error

刚刚使用 google 开设了一个结算帐户以使用地点自动完成 api。创建密钥并启用 API。现在,当我开始输入 AutoCompleteTextView 时,使用下面的代码会出现错误。第一个错误是

java.util.concurrent.ExecutionException: com.google.android.gms.common.api.ApiException: 13: ERROR

这是 java 代码。我从 google link 到 GitHub 得到的 PlaceAutocompleteAdapter 代码,所以假设我不需要 post 该代码,但这里是 link 添加到该代码以备不时之需。 https://github.com/googlesamples/android-play-places/tree/master/PlaceCompleteAdapter.

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.AutoCompleteTextView;

import com.google.android.gms.location.places.GeoDataClient;
import com.google.android.gms.location.places.Places;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.LatLngBounds;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        PlaceAutocompleteAdapter mPlaceAutocompleteAdapter;
        GeoDataClient mGeoDataClient = Places.getGeoDataClient(this, null);
        LatLngBounds LAT_LNG_BOUNDS = new LatLngBounds(new LatLng(-40, -168), new LatLng(71, 136));
        mPlaceAutocompleteAdapter = new PlaceAutocompleteAdapter(this, mGeoDataClient, LAT_LNG_BOUNDS, null);

        AutoCompleteTextView autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompEditText);
        autoCompleteTextView.setAdapter(mPlaceAutocompleteAdapter);
    }
}

这是清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
        package="com.example.me.testautocomplete">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

和logcat输出错误开始的地方

    08-13 21:43:29.872 5015-5055/com.example.me.testautocomplete W/System.err: java.util.concurrent.ExecutionException: com.google.android.gms.common.api.ApiException: 13: ERROR
08-13 21:43:29.902 5015-5055/com.example.me.testautocomplete W/System.err:     at com.google.android.gms.tasks.Tasks.zzb(Unknown Source)
        at com.google.android.gms.tasks.Tasks.await(Unknown Source)
08-13 21:43:29.922 5015-5055/com.example.me.testautocomplete W/System.err:     at com.example.me.testautocomplete.PlaceAutocompleteAdapter.getAutocomplete(PlaceAutocompleteAdapter.java:212)
        at com.example.me.testautocomplete.PlaceAutocompleteAdapter.access[=14=]0(PlaceAutocompleteAdapter.java:53)
        at com.example.me.testautocomplete.PlaceAutocompleteAdapter.performFiltering(PlaceAutocompleteAdapter.java:147)
        at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.os.HandlerThread.run(HandlerThread.java:61)
08-13 21:43:29.932 5015-5055/com.example.me.testautocomplete W/System.err: Caused by: com.google.android.gms.common.api.ApiException: 13: ERROR
08-13 21:43:29.952 5015-5055/com.example.me.testautocomplete W/System.err:     at com.google.android.gms.common.internal.zzb.zzx(Unknown Source)
        at com.google.android.gms.common.internal.zzbj.zzy(Unknown Source)
        at com.google.android.gms.common.internal.zzbk.zzp(Unknown Source)
        at com.google.android.gms.common.api.internal.zzs.zzc(Unknown Source)
        at com.google.android.gms.common.api.internal.zzs.setResult(Unknown Source)
        at com.google.android.gms.location.places.zzm.zzap(Unknown Source)
        at com.google.android.gms.location.places.internal.zzx.onTransact(Unknown Source)
        at android.os.Binder.execTransact(Binder.java:404)
08-13 21:43:29.972 5015-5055/com.example.me.testautocomplete W/System.err:     at dalvik.system.NativeStart.run(Native Method)
08-13 21:43:30.102 5015-5055/com.example.me.testautocomplete D/dalvikvm: GC_FOR_ALLOC freed 221K, 9% free 3627K/3944K, paused 11ms, total 13ms
08-13 21:43:30.252 5015-5055/com.example.me.testautocomplete E/PlaceAutocomplete: Error getting autocomplete prediction API call
    com.google.android.gms.tasks.RuntimeExecutionException: com.google.android.gms.common.api.ApiException: 13: ERROR
        at com.google.android.gms.tasks.zzn.getResult(Unknown Source)
        at com.example.me.testautocomplete.PlaceAutocompleteAdapter.getAutocomplete(PlaceAutocompleteAdapter.java:218)
        at com.example.me.testautocomplete.PlaceAutocompleteAdapter.access[=14=]0(PlaceAutocompleteAdapter.java:53)
        at com.example.me.testautocomplete.PlaceAutocompleteAdapter.performFiltering(PlaceAutocompleteAdapter.java:147)
        at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.os.HandlerThread.run(HandlerThread.java:61)
     Caused by: com.google.android.gms.common.api.ApiException: 13: ERROR
        at com.google.android.gms.common.internal.zzb.zzx(Unknown Source)
        at com.google.android.gms.common.internal.zzbj.zzy(Unknown Source)
        at com.google.android.gms.common.internal.zzbk.zzp(Unknown Source)
        at com.google.android.gms.common.api.internal.zzs.zzc(Unknown Source)
        at com.google.android.gms.common.api.internal.zzs.setResult(Unknown Source)
        at com.google.android.gms.location.places.zzm.zzap(Unknown Source)
        at com.google.android.gms.location.places.internal.zzx.onTransact(Unknown Source)
        at android.os.Binder.execTransact(Binder.java:404)
        at dalvik.system.NativeStart.run(Native Method)

谢谢

解决了我的问题。看起来我在 AndroidManifest.xml.

中遗漏了这个
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

    <meta-data
        android:name="com.google.android.geo.API_KEY"
        android:value="YOUR_KEY" />
</application>

关键是将它放在 <application> 元素中。