android - 我在 Android 中使用新的 Places SDK。 AutocompleteSupportFragment,它要求我将 Activity 添加到 list 中

标签 android android-fragments google-places-api

我正在使用新的 places SDK。我的构建.gradle:

implementation 'com.google.android.libraries.places:places:1.0.0'

我的代码:

 FragmentManager supportFragmentManager = getChildFragmentManager();
 FragmentTransaction ft = supportFragmentManager.beginTransaction();
 if(mAutocompleteFragment == null) {
      mAutocompleteFragment = new AutocompleteSupportFragment();
      ft.add(R.id.places_container, mAutocompleteFragment, AutocompleteSupportFragment.class.getSimpleName());
      ft.commit();
    }

List<Place.Field> places = new ArrayList<>();
places.add(Place.Field.ID);
places.add(Place.Field.NAME);
places.add(Place.Field.ADDRESS);
mAutocompleteFragment.setPlaceFields(places); //throws NPE getView() is null

抛出 NPE,因为它试图在最后一个方法中设置启用 View 并且 View 为空。我怀疑 fragment 的生命周期没有被调用(onViewCreated)。我将如何强制生命周期或等待 fragment 完全循环?

但是,当我选择 autocompletesupportfragment 时,我得到:

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.activehours.debug/com.google.android.libraries.places.widget.AutocompleteActivity}; have you declared this activity in your AndroidManifest.xml?

这是我的 list :

    <activity
    android:name="com.google.android.libraries.places.widget.AutocompleteActivity"
        android:theme="@style/PlacesAutocompleteThemeOverlay">
    </activity>

documentation没有提到在我的 list 中包括该 Activity 。帮助!

编辑:我通过这样扩展类来绕过 NPE:

public class AHAutocompleteSupportFragment extends AutocompleteSupportFragment 
 {

  @Override
  public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    List<Place.Field> places = new ArrayList<>();
    places.add(Place.Field.ID);
    places.add(Place.Field.NAME);
    places.add(Place.Field.ADDRESS);
    this.setPlaceFields(places);
  }
}

但是,我仍然收到“未在 list 中声明的​​ Activity ”。

最佳答案

您不必自己实例化 fragment ...

mAutocompleteFragment = new AutocompleteSupportFragment();

您是否有任何理由不只是在现有 Activity 中使用该 fragment ?

下面是我在我的应用程序中使用新的 AutocompleteSupportFragment 的代码,它运行良好。

<android.support.v7.widget.CardView
                android:layout_width="0dp"
                android:layout_height="@dimen/shop_search_search_area_height"
                android:layout_marginTop="@dimen/shop_search_search_area_margin"
                android:layout_marginStart="@dimen/shop_search_search_area_margin"
                app:cardElevation="8dp"
                android:layout_weight="4">

                <fragment
                    android:id="@+id/shop_search_autocomplete_fragment"
                    android:name="com.google.android.libraries.places.widget.AutocompleteSupportFragment"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent">

                    <ImageView
                        android:id="@+id/shop_search_voice_icon"
                        android:layout_width="@dimen/shop_search_mic_size"
                        android:layout_height="@dimen/shop_search_mic_size"
                        android:layout_marginEnd="@dimen/shop_search_mic_margin_right"
                        android:contentDescription="@null"
                        android:onClick="@{() -> model.voicePlaceSearch()}"
                        android:src="@drawable/ic_mic_24dp_gray" />
                </fragment>
            </android.support.v7.widget.CardView>

然后为了在我的代码中掌握它,我将使用教程提供的相同代码...

AutocompleteSupportFragment autocompleteFragment = (AutocompleteSupportFragment)
        getSupportFragmentManager().findFragmentById(R.id.shop_search_autocomplete_fragment);

关于android - 我在 Android 中使用新的 Places SDK。 AutocompleteSupportFragment,它要求我将 Activity 添加到 list 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54774648/

相关文章:

java - 将 MySql 中两个表的数据导入 Android 应用程序

javascript - 仅基于 2 个 WayPoint 显示 Google Places 搜索结果

java - Android - 错误膨胀类 fragment - XML 文件

javascript - Google 的 Places API 和 JQuery 请求 - 来源 http ://localhost is not allowed by Access-Control-Allow-Origin

javascript - 使用 Google 地方信息获取郊区的邮政编码

android - IndexOutOfBoundsException:获取菜单项(溢出图标)项的标题时

Android 服务到 Activity 通信性能

android - 启动新 Activity 时出现 OutOfMemoryError

Android NewBie - setAdapter ListView 面临的问题

android - IllegalStateException:在使用 ViewPager onSaveInstanceState 后无法执行此操作