添加有错误的视图-重复的ID,标签为空或父ID为另一个片段

问题描述

我有一个Main Fragment,其中包含嵌套的nav主机片段。由于动态问题,我选择使用添加视图将导航主机片段添加到主片段中。

我的问题是,第一次加载该片段没有问题。但是,当我转到另一个片段并回到该片段时。它显示“重复的ID,标签null或父ID,以及NAV主机片段中的另一个片段”错误消息。

以前有人遇到过类似的问题吗?您能分享您的经验吗?非常感谢。

XML

MainFragment

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" />

layouttoadd

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <fragment
        android:id="@+id/nestednav"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/navgraph" />
</LinearLayout>

Java

MainFragment

public class MainFragment extends Fragment {

    View root;
    public View onCreateView(@NonNull LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {

        root = inflater.inflate(R.layout.MainFragment,container,false);
        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view,@Nullable Bundle savedInstanceState) {
        super.onViewCreated(view,savedInstanceState);

        LinearLayout Layout = view.findViewById(R.id.layout);
        View addlayout= layoutInflater.inflate(R.layout.layouttoadd,null);
        Layout.addView(addlayout);

    }

}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)