适用于Android Place Picker的Google Places API不起作用

我无法在我的应用程序中使用地方select器。 然后API工作就停了一天,没有任何解释。select器出现很短的时间,然后简单地closures,没有任何例外和日志。 使用requestCode = 1,resultCode = 2(错误代码)和无数据调用onActivityResult函数。 来自谷歌的示例代码做同样的事情。 我已经尝试了所有的工作。 有人遇到类似的东西吗? 请帮助。

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.easy.parcelapp." android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="22" /> <uses-feature android:glEsVersion="0x00020000" android:required="true" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-permission android:name="android.permission.VIBRATE" /> <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> <uses-permission android:name="android.permission.ACCESS_LOCATION_EXTRA_COMMANDS" /> <uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.RECEIVE_SMS" /> <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.RECORD_VIDEO" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> <uses-permission android:name="android.permission.READ_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_CONTACTS" /> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.GET_ACCOUNTS" /> <uses-permission android:name="android.permission.BROADCAST_STICKY" /> <uses-permission android:name="android.permission.WAKE_LOCK" /> <uses-permission android:name="android.permission.READ_PHONE_STATE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/> <application android:name=".EasiParcelApplication" android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="AIzaSyDEcV-My-Actual-API-Key-goes-here" /> <activity android:name=".MainActivity" android:label="@string/app_name" android:theme="@android:style/Theme.Holo.Light.NoActionBar" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name=".RegistrationActivity" android:label="@string/title_activity_register" android:parentActivityName=".MainActivity" > <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.easiparcel.MainActivity" /> </activity> <activity android:name=".JobListingActivity" android:label="@string/title_activity_listing" > </activity> <activity android:name=".NewJobActivity" android:label="@string/title_activity_new_job" android:parentActivityName=".JobListingActivity" > <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.easiparcel.JobListingActivity" /> </activity> <activity android:name=".NewJobConfirmation" android:label="@string/title_activity_new_job_confirmation" android:parentActivityName=".NewJobActivity" > <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.easiparcel.NewJobActivity" /> </activity> <activity android:name=".JobDetails" android:label="@string/title_activity_job_details" android:parentActivityName=".JobListingActivity" > <meta-data android:name="android.support.PARENT_ACTIVITY" android:value="com.easiparcel.JobListingActivity" /> </activity> </application> </manifest> 

Activity.java

  public void HandleSenderAddressPickerClicked(View view) { int PLACE_PICKER_REQUEST = 1; LoadPlacePicker(PLACE_PICKER_REQUEST); } public void HandleRecieverAddressPickerClicked(View view) { int PLACE_PICKER_REQUEST = 2; LoadPlacePicker(PLACE_PICKER_REQUEST); } private void LoadPlacePicker(int PlacePickerRequest) { try { PlacePicker.IntentBuilder intentBuilder = new PlacePicker.IntentBuilder(); Intent intent = intentBuilder.build(NewJobActivity.this); // Start the Intent by requesting a result, identified by a request code. startActivityForResult(intent, PlacePickerRequest); } catch (GooglePlayServicesRepairableException e) { GooglePlayServicesUtil .getErrorDialog(e.getConnectionStatusCode(), NewJobActivity.this, 0); } catch (GooglePlayServicesNotAvailableException e) { Toast.makeText(NewJobActivity.this, "Google Play Services is not available.", Toast.LENGTH_LONG) .show(); } } protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (requestCode == 1) { if (resultCode == RESULT_OK) { Place place = PlacePicker.getPlace(data, this); String toastMsg = String.format("Sender Place: %s", place.getName()); Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show(); } } //Reciever if (requestCode == 2) { if (resultCode == RESULT_OK) { Place place = PlacePicker.getPlace(data, this); String toastMsg = String.format("Receiver Place: %s", place.getName()); Toast.makeText(this, toastMsg, Toast.LENGTH_LONG).show(); } } } 

Solutions Collecting From Web of "适用于Android Place Picker的Google Places API不起作用"

我想出了自己的问题的答案。

如果有其他人遇到同样的问题,请确保您在开发者控制台中启用“Places API for Android”,而不是“Places API”“Places API for Android”不会显示在“APIs&Auth / APIs”下面,因为它还不是stream行的API(还)。 您将不得不使用API​​search框进行search。

而不是这个:

 <meta-data android:name="com.google.android.maps.v2.API_KEY" android:value="your_key"/> 

添加这个:

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

它为我工作!

检查你的构build变种。

如果您从release切换回debug ,通常也会得到结果代码2

切换回release为我做了伎俩。