我正在尝试使用 Google 的地点自动完成 ( https://developers.google.com/places/android-api/autocomplete ) 在我的应用程序中的 fragment 中实现。但是当从这个 fragment 导航到其他 fragment 然后返回到这个 fragment 时,我遇到了这个错误
Caused by: java.lang.IllegalArgumentException: Binary XML file line #30: Duplicate id 0x7f0f010f, tag null, or parent id 0x7f0f00c0 with another fragment for com.google.android.gms.location.places.ui.PlaceAutocompleteFragment
at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2148)
这是java代码
private PlaceAutocompleteFragment mSearchPAF;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.some_layout, container, false);
mSearchLocationPAF = (PlaceAutocompleteFragment) parentActivity.getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
}
这是 XML 文件
<RelativeLayout
android:id="@+id/someRelativeLayout"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="30dp"
android:background="@drawable/some_drawable">
<fragment
android:id="@+id/place_autocomplete_fragment"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
我在同一个 java 和 xml 文件中还有另一个 map fragment ,但我设法通过遵循这篇文章中的答案( Duplicate ID, tag null, or parent id with another fragment for com.google.android.gms.maps.MapFragment )来解决它,但我找不到此地点自动完成 fragment 的任何解决方案
最佳答案
我自己通过遵循此处的答案之一解决了这个问题 Duplicate ID, tag null, or parent id with another fragment for com.google.android.gms.maps.MapFragment
private PlaceAutocompleteFragment mSearchPAF;
@Override
public void onDestroyView() {
super.onDestroyView();
PlaceAutocompleteFragment f = (PlaceAutocompleteFragment) getFragmentManager()
.findFragmentById(R.id.place_autocomplete_fragment);
if (f != null)
getFragmentManager().beginTransaction().remove(f).commit();
}
关于java - 如何将 Google 的 PlaceAutoCompleteFragment 实现到扩展 Fragment 的类中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44234563/