我正在尝试在我的项目中使用Places API for Android,但它会抛出此异常。
由于它已弃用,我正在使用Google设置的兼容性库,但我无法使其正常工作,我已经启用了Places API,为它设置了一个不受限制的API(仅用于测试目的)并启用了结算功能。我还关注了Google为迁移过程启用的文档。
这是我的gradle.app文件:
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.google.android.gms:play-services-auth:16.0.1'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.android.support:design:27.1.1'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.google.android.libraries.places:places-compat:1.1.0'
implementation 'com.android.support:multidex:1.0.3'
}
这是我在Manifest中的元数据,它在关闭Application标签之前被剪断了
<meta-data android:name="com.google.android.geo.API_KEY"
android:value="@string/google_place_api"/>
片段中的导入我正在使用
import com.google.android.libraries.places.compat.Place;
import com.google.android.libraries.places.compat.ui.PlacePicker;
我正在使用的代码
PlacePicker.IntentBuilder builder = new PlacePicker.IntentBuilder();
Intent intent;
try
{
intent = builder.build(getActivity());
startActivityForResult(intent, REQUEST_CODE);
}
catch (GooglePlayServicesRepairableException e)
{
e.printStackTrace();
}
catch (GooglePlayServicesNotAvailableException e)
{
e.printStackTrace();
}
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE && resultCode == RESULT_OK)
{
TextView newLocationText = v.findViewById(R.id.newAddress);
Place place = PlacePicker.getPlace(data,v.getContext());
String address = String.format("Place: %s",place.getAddress());
newLocationText.setText(address);
}
}
当我运行应用程序时,我在logcat中收到此消息:
2019-04-22 00:31:50.114 2381-5745/? E/Places: Places API for Android does not seem to be enabled for your app. See https://developers.google.com/places/android/signup for more details.
2019-04-22 00:31:50.122 7584-7584/? E/Places: Place Picker closing due to PLACES_API_ACCESS_NOT_CONFIGURED
2019-04-22 00:31:50.312 2381-5745/? E/Places: Places API for Android does not seem to be enabled for your app. See https://developers.google.com/places/android/signup for more details.
2019-04-22 00:31:50.313 2381-5745/? E/AsyncOperation: serviceID=65, operation=SearchPlaces
OperationException[Status{statusCode=PLACES_API_ACCESS_NOT_CONFIGURED, resolution=null}]
面对同样的问题,尝试了一切,但没有任何效果。然后我尝试创建一个新的API密钥。这个解决方案对我有用!