【问题标题】:Android Place Autocomplete Fragment closing on its ownAndroid Place Autocomplete Fragment 自行关闭
【发布时间】:2017-04-18 08:15:29
【问题描述】:

我第一次尝试实现放置自动完成片段并面临一个问题。当我开始输入时,在第一个字母之后它开始搜索,而不是显示结果,它的(片段)只是消失了.. 我得到的是 状态{statusCode=PLACES_API_ACCESS_NOT_CONFIGURED, resolution=null}

Manifest.xml

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

Xml_layout

<TextView
    android:text="TextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_marginTop="14dp"
    android:id="@+id/Area"
    android:onClick="Onselection"
    android:layout_alignParentEnd="true"
    android:layout_alignParentStart="true" />
<fragment
        android:id="@+id/place_autocomplete_fragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
        android:layout_below="@+id/Area"
        android:layout_alignParentStart="true" />

public class test extends AppCompatActivity implements PlaceSelectionListener {


    private static final String LOG_TAG = "PlaceSelectionListener";
    private static final LatLngBounds BOUNDS_MOUNTAIN_VIEW = new LatLngBounds(
            new LatLng(-85, -180), new LatLng(85, 180));
    private static final int REQUEST_SELECT_PLACE = 1000;
    TextView locationtext ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_testtttt);
        locationtext = (TextView) findViewById(R.id.Arear) ;

        PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
                getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

        autocompleteFragment.setOnPlaceSelectedListener(this);

        AutocompleteFilter typeFilter = new AutocompleteFilter.Builder()
                .setTypeFilter(AutocompleteFilter.TYPE_FILTER_ADDRESS)
                .build();
        autocompleteFragment.setFilter(typeFilter);


    }


    public  void Onselection(View v)
    {
        try {
            Intent intent = new PlaceAutocomplete.IntentBuilder
                    (PlaceAutocomplete.MODE_OVERLAY)
                    .setBoundsBias(BOUNDS_MOUNTAIN_VIEW)
                    .build(testtttt.this);
            startActivityForResult(intent, REQUEST_SELECT_PLACE);
        } catch (GooglePlayServicesRepairableException |
                GooglePlayServicesNotAvailableException e) {
            e.printStackTrace();
        }
    }


    @Override
    public void onPlaceSelected(Place place) {

    }

    @Override
    public void onError(Status status) {

    }

出现点击片段

片段自行消失/关闭

【问题讨论】:

标签: android android-fragments autocomplete


【解决方案1】:

如果您已将 api 添加为受限,请在允许的 API 中添加地点 api

【讨论】:

    【解决方案2】:

    另一个可能的原因是您尚未为您的项目启用结算功能。我就是这样。我尝试了此页面中的所有解决方案,但在我输入字符后,place-autocomplete-fragment 仍然会自行关闭。

    所以我真的回到了基础并去了Places SDK for Android Getting Started page,它在该页面上提到我们需要在使用 Places SDK for Android 之前启用计费。这解决了我的问题。

    要启用结算功能,请转至the billing page

    【讨论】:

    • 放置api需要的计费账户?
    【解决方案3】:

    我认为您应该禁用对 api 密钥的限制。这可能不是最安全的方法,但对我有用。

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题,然后我偶然发现了这条消息 如果你想自己检查,这里是链接。 Places SDK for Android
      长话短说,我迁移到了新的 APIS,这里给出了它的说明。 Migrating to the new Places SDK Client

      就我而言 我的地图位于 Fragment 上,而搜索位于地图之上

      build.gradle

          implementation "com.google.android.gms:play-services-location:16.0.0"
          implementation "com.google.android.gms:play-services-maps:16.1.0"
          implementation 'com.google.android.libraries.places:places:1.0.0'
      

      请注意 API 是全新的 1.0.0 的地方。 这些 API 给了我:
      com.google.android.gms.maps.SupportMapFragment 用于 MAP
      com.google.android.libraries.places.widget.AutocompleteSupportFragment 用于搜索栏

          <fragment
              android:id="@+id/map"
              android:name="com.google.android.gms.maps.SupportMapFragment"
              android:layout_width="0dp"
              android:layout_height="0dp"
              android:layout_marginBottom="16dp"
              android:layout_marginTop="16dp"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent" />
      
          <fragment
              android:id="@+id/f_auto_complete"
              android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              android:layout_marginStart="16dp"
              android:layout_marginTop="16dp"
              android:layout_marginEnd="16dp"
              android:minHeight="@dimen/size_search_view"
              app:layout_constraintEnd_toEndOf="@+id/map"
              app:layout_constraintStart_toStartOf="@id/map"
              app:layout_constraintTop_toTopOf="@id/map" />
      

      片段

      private fun initAutoCompleteView() {
              activity?.let { a ->
                  if (!Places.isInitialized()) {
                      Places.initialize(a.applicationContext, "YOUR API KEY")
                  }
                  fragment = childFragmentManager.findFragmentById(R.id.f_auto_complete) as AutocompleteSupportFragment
                  fragment?.let {                    
                      it.setPlaceFields(Arrays.asList(Place.Field.ID, Place.Field.NAME, Place.Field.LAT_LNG))
                      it.setOnPlaceSelectedListener(object : PlaceSelectionListener {
                          override fun onPlaceSelected(place: Place) {
                              Log.i("Places", place.name)
      
                              }
                          }
      
                          override fun onError(status: Status) {
                              Log.i("Places", "An error occurred: $status")
                          }
                      })
                  }
              }
          }
      

      为了让生活更轻松,我还添加了导入,以防万一任何类不适合。

      import com.google.android.libraries.places.api.Places
      import com.google.android.libraries.places.api.model.Place
      import com.google.android.libraries.places.widget.AutocompleteSupportFragment
      import com.google.android.libraries.places.widget.listener.PlaceSelectionListener
      

      【讨论】:

        【解决方案5】:

        由于 SHA1 指纹,有时可能会出现问题,在我的情况下,将 debug sha1 key 替换为 release sha1 app 工作正常

        【讨论】:

          【解决方案6】:

          rand_mem_RAM 是对的。
          花了我 2 天的时间谷歌搜索,可能绕过了答案,因为它太简单了。

          1. 转到 console.developers.google.com
          2. 选择与 Google API 密钥关联的项目
          3. 为 android 启用 google 地方 API 3a。单击网页顶部的第一个蓝色按钮,这将打开下一个网页 3b。接下来,单击下一页顶部的第二个蓝色按钮 然后测试您的设备。

          本来会投票,但不能作为客人投票。

          【讨论】:

          • 您的答案与上一个答案相似。
          【解决方案7】:

          在 Android 上使用 Google API 进行开发时,这是一种非常有趣的体验。您需要做的就是:

          1. 转到console.developers.google.com
          2. 选择与 Google API 密钥关联的项目
          3. 为 android 启用 Google Places API

          就是这样。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2019-03-15
            • 2015-08-06
            • 2020-08-10
            • 2018-04-22
            相关资源
            最近更新 更多