【问题标题】:Google Places Auto Complete fragment causes duplicate id errorGoogle Places Autocomplete 片段导致重复 ID 错误
【发布时间】:2017-12-03 17:02:34
【问题描述】:

您好,我在我的应用中使用了谷歌的地点自动完成 API。我需要谷歌将片段放在另一个片段中并让它工作。基本上我有一个带有 3 个图像视图的选项卡,当您单击一个时,一个片段将填充主框架布局。最初,如果我单击显示带有自动完成位置片段的片段的 imagview,它会起作用。但是,如果我转到另一个选项卡然后返回它,我会在 google 位置自动完成片段上收到重复的 id 错误。

这是包含自动完成片段的片段的 xml:

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:drawable/dialog_holo_light_frame"
    android:orientation="vertical">

    <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="42dp"
        android:background="@android:drawable/dialog_frame"
        tools:layout="@layout/place_autocomplete_fragment" /> 

这是包含自动完成片段的片段中的代码:

this.locationSearchBar = (PlaceAutocompleteFragment)getActivity().getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);

这是我得到的错误:

Duplicate id 0x7f0800dc, tag null, or parent id 0x7f0800f6 with another fragment for com.google.android.gms.location.places.ui.PlaceAutocompleteFragment

我阅读了其他类似的问题,但他们的解决方案都不适合我。任何帮助将不胜感激。

【问题讨论】:

  • 您是否尝试过简单地更改片段 ID?或者您使用的每个 id 都会出现错误?因为您有一个使用tools:layout=@layout/place_autocomplete_fragment 引用的同名布局
  • 您正在尝试查找 Activity 的片段...不是来自当前片段
  • 我每个 id 都得到它
  • @Mat tools: 属性在编译时被移除
  • @cricket_007 第二次放大片段布局时出现错误。就好像自动完成片段永远不会被破坏,即使它所在的片段是。

标签: android android-fragments android-nested-fragment


【解决方案1】:

尝试添加此代码:

@Override
public void onDestroy() {
    super.onDestroy();
    if(mAutocompleteFragment != null && getActivity() != null && !getActivity().isFinishing()) {
        getActivity().getFragmentManager().beginTransaction().remove(mAutocompleteFragment).commit();
    }
}

我相信你也可以把它放在 onDestroyView() 中。在将 AutocompleteFragment 添加到同一个 Activity 之前,需要从每个 ACTIVITY 中删除它。当我创建并随后销毁添加了 AutocompleteFragment 的 Fragment 然后尝试再次创建相同的 Fragment 而没有销毁它在其中运行的 Activity 时,这对我来说是一个问题。

【讨论】:

  • 谢谢为我工作,但我需要调用它 onDestroyView();谢谢大佬
猜你喜欢
  • 2022-01-17
  • 2017-10-05
  • 2017-09-12
  • 2016-09-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-09
  • 2017-01-23
相关资源
最近更新 更多