我正在使用新的 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/