谷歌地方自动完成活动立即关闭自动

时间:2018-01-23 07:43:27

标签: android autocomplete google-places-api

我在我的应用中使用google place api进行地方自动填充,但它会立即自动关闭。 我尝试了碎片和活动,但两者都很接近。

片段代码:

turbolinks

活动代码:

  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.
        }
    }
}

我使用谷歌地图api和清单:

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

7 个答案:

答案 0 :(得分:0)

根据您的错误 Places API 未正确配置。

按照步骤进行配置

https://developers.google.com/places/android-api/

答案 1 :(得分:0)

转到google developer console,然后从那里获得api密钥

请根据错误提示从那里启用places API。

GoogleDevelopersConsole->SignIn->Dashboard->On the top Enable API's and Services->Click that->Enable google places API

多数民众赞成!!

答案 2 :(得分:0)

问题是我在其他项目中创建了地方api,我使用谷歌地图的api,这是在谷歌控制台的不同项目中创建的。解决方案是你必须在同一个项目中创建google palace api,如果你已经在你的应用程序中使用任何谷歌API。并且只在清单文件中使用一个。

答案 3 :(得分:0)

发生这种情况,你没有包含谷歌地方的api密钥。 如果您没有启用它,只需去谷歌开发者控制台注册您的应用程序并启用谷歌地方API。 #Peace

答案 4 :(得分:0)

我在片段顶部使用了地图,其行为是相同的。单击搜索后,弹出窗口便消失了。当我迁移到新的API时,此问题已得到修复,这是我在此处添加的完整答案
Android Place Autocomplete Fragment closing on its own

如果您想自己检查一下,请点击以下链接:
Places SDK for Android
Migrating to new Places SDK for Android

答案 5 :(得分:0)

您可能必须初始化地方

// Initialize Places.
    if (!Places.isInitialized()) {
        Places.initialize(getApplicationContext(),"Your API KEY");
    }

答案 6 :(得分:0)

查找此错误是由于未在Google Cloud中启用计费功能引起的。尝试设置帐单,它将解决。