我在我的应用程序中使用了google place api的place自动完成功能,但它会立即自动关闭。我尝试了片断和活动,但两者都很接近。
片段代码:
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
}
@Override
public void onError(Status status) {
// TODO: Handle the error.
}
});活动代码:
public void findPlace(View v) {
try {
Intent intent =
new PlaceAutocomplete
.IntentBuilder(PlaceAutocomplete.MODE_FULLSCREEN)
.build(this);
startActivityForResult(intent, 1);
} catch (GooglePlayServicesRepairableException e) {
// TODO: Handle the error.
} catch (GooglePlayServicesNotAvailableException e) {
// TODO: Handle the error.
}
}
// A place has been received; use requestCode to track the request.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
// retrive the data by using getPlace() method.
Place place = PlaceAutocomplete.getPlace(this, data);
//Log.e("Tag", "Place: " + place.getAddress() + place.getPhoneNumber());
// cLocation.setText(place.getName()+",\n"+
// place.getAddress() +"\n" + place.getPhoneNumber());
} else if (resultCode == PlaceAutocomplete.RESULT_ERROR) {
Status status = PlaceAutocomplete.getStatus(this, data);
// TODO: Handle the error.
//Log.e("Tag", status.getStatusMessage());
} else if (resultCode == RESULT_CANCELED) {
// The user canceled the operation.
}
}
}当我点击按钮新的活动打开但突然关闭,有什么原因吗?
日志错误:
01-23 13:19:55.614 904-2734/? E/Volley: [146] BasicNetwork.performRequest: Unexpected response code 403 for https://www.googleapis.com/placesandroid/v1/autocompleteWidget?key=AIzaSyDBQ4zMPX740wUMlIyv6KMrjIY3hj2GN2Y
01-23 13:19:55.624 904-5307/? 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.
01-23 13:19:55.625 904-5307/? E/AsyncOperation: serviceID=65, operation=AutocompleteWidgetQuota
OperationException[Status{statusCode=PLACES_API_ACCESS_NOT_CONFIGURED, resolution=null}]
at anjf.b(:com.google.android.gms@11951034:1)
at anis.a(:com.google.android.gms@11951034:7)
at qez.run(:com.google.android.gms@11951034:11)
at kdf.run(:com.google.android.gms@11951034:26)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at kiq.run(:com.google.android.gms@11951034)
at java.lang.Thread.run(Thread.java:841)
01-23 13:19:55.626 11046-11046/? E/Places: Autocomplete widget closing due to PLACES_API_ACCESS_NOT_CONFIGURED
01-23 13:20:22.732 21633-21633/? E/cutils-trace: Error opening trace file: No such file or directory (2)
01-23 13:20:22.914 21633-21633/? E/memtrack: Couldn't load memtrack module (No such file or directory)
01-23 13:20:22.914 21633-21633/? E/android.os.Debug: failed to load memtrack module: -2
01-23 13:20:26.396 689-961/? E/Sensors: new acc setDelay handle(0),ns(20000000) err! go to hwmsen
01-23 13:20:26.421 904-1275/? E/CellLocation: create GsmCellLocation
01-23 13:20:27.002 689-700/? E/Sensors: new acc setDelay handle(0),ns(66667000) err! go to hwmsen我使用谷歌地图api和manifest:
<meta-data android:name="com.google.android.geo.API_KEY" android:value="@string/google_maps_key" /> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" />发布于 2019-05-21 14:36:41
您可能需要初始化位置
// Initialize Places.
if (!Places.isInitialized()) {
Places.initialize(getApplicationContext(),"Your API KEY");
}发布于 2020-09-08 21:13:33
看起来这个错误是由于在Google Cloud中没有启用计费。尝试设置帐单,它将解决。
发布于 2018-01-23 15:57:11
https://stackoverflow.com/questions/48396524
复制相似问题