android - 二进制 XML 文件第 26 行 : Duplicate id, 标记为空,或带有另一个 fragment 的父 ID

标签 android android-fragments android-nested-fragment

我正在尝试将一个 fragment 插入另一个 fragment ,并且我已经成功地做到了这一点,直到我第一次吃完主 fragment ,它才开始工作,但是当我试图重新加载 fragment 时,应用程序崩溃了,并且我有这个错误:

Caused by: java.lang.IllegalArgumentException: Binary XML file line #26:
Duplicate id 0x7f0e00e2, tag null, or parent id 0xffffffff with another
fragment for com.google.android.gms.location.places.ui.PlaceAutocompleteFragment

这是我的 fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View  rootView = inflater.inflate(R.layout.source_destination,container, false);
PlaceAutocompleteFragment sourcr_autocompleteFragment = (PlaceAutocompleteFragment)
          getActivity().getFragmentManager()
.findFragmentById(R.id.sourcr_autocomplete_fragment);
return rootView;

// List item

}

这是我的 XML

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical"
        android:padding="10dp"
        android:background="@color/colorPrimary"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
       <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="@dimen/margin_medium"
            android:layout_marginBottom="@dimen/margin_medium">

            <fragment
                android:id="@+id/sourcr_autocomplete_fragment"    
                android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"           
              android:layout_width="match_parent"
              android:layout_height="wrap_content" />

        </android.support.v7.widget.CardView>
    </LinearLayout>

最佳答案

我刚遇到同样的问题,可以通过删除 onDismiss 回调中的 fragment 来解决。

@Override
public void onDismiss(DialogInterface dialog) {
    super.onDismiss(dialog);

    if (getActivity() != null) {
        FragmentManager fragmentManager = getActivity().getFragmentManager();
        Fragment fragment = fragmentManager.findFragmentById(R.id.sourcr_autocomplete_fragment);
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.remove(fragment);
        fragmentTransaction.commit();
    }
}

关于android - 二进制 XML 文件第 26 行 : Duplicate id, 标记为空,或带有另一个 fragment 的父 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37959617/

相关文章:

android - 无需轻弹即可管理 fragment 后栈流

android - fragment 递归进入 executePendingTransactions 错误

android - 使用内部 fragment 设置 InteractionListener

android - 如何从 build.gradle 中的 json 配置文件创建 ProductFlavors?

android - LiveData onChanged 方法仅在第一次调用

android - 将 Web 应用程序连接到 mysql 数据库

Android 在调用 ft.replace 时为同一 fragment 返回不同的 ViewModel 实例

android - 三指触摸后关闭 Android 应用程序

android - 当我在 Android 中第二次移动到 fragment 时,PagerAdapter 没有在 fragment 内部调用

Android 卖家退款和取消 - 用户还能升级他们的应用吗?