片段布局不添加更多视图

Fragment Layout Not Adding Up More Views

我正在尝试在我的片段布局中添加一个 FrameLayout,只是为了保留 space 以编程方式添加另一个片段。这是代码:

片段XML布局

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

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/detail"
        android:text="" />

    <FrameLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fragment_container"/>

</LinearLayout>

但问题是,当我使用 FragmentTransaction 替换 带有片段的 FrameLayout 时,它给出 错误:

FATAL EXCEPTION: main
              Process: com.platform.apple, PID: 2448
              java.lang.IllegalArgumentException: No view found for id 0x7f080005 (com.platform.apple:id/fragment_container) for fragment Stopwatch1Fragment{e0be34c #0 id=0x7f080005}

这是片段 class 代码:

片段Class代码

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    Stopwatch1Fragment stopwatch1Fragment = new Stopwatch1Fragment();
    transaction.replace(R.id.fragment_container, stopwatch1Fragment);
    transaction.addToBackStack(null);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    transaction.commit();
    return inflater.inflate(R.layout.fragment_apple_detail, container, false);
}

现在,当我尝试使用 R.id.detail 而不是 R.id.fragment_container 时, 片段 Class 发现 TextView 而当我在 片段的 xml 布局 中的 FrameLayout 之后创建另一个 TextView 并尝试使用它时,它仍然不起作用(片段没有' t 通过它的 Id 找到视图)。所以看起来,只有 first view(R.id.detail) 在 Fragment class在那之后找不到任何其他添加的视图。请帮忙!

尝试通过以下方法复制 onCreateView 代码

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

    View root = inflater.inflate(R.layout.fragment_apple_detail, container, false);

    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    Stopwatch1Fragment stopwatch1Fragment = new Stopwatch1Fragment();
    transaction.replace(R.id.fragment_container, stopwatch1Fragment);
    transaction.addToBackStack(null);
    transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
    transaction.commit();

    return root;
}

将宽度和高度设置为片段中的匹配父元素 xml 如下布局

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

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/detail"
    android:text="" />

<FrameLayout
    android:layout_width="match_content"
    android:layout_height="match_content"
    android:id="@+id/fragment_container"/>

然后像下面这样以编程方式初始化片段

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

View root = inflater.inflate(R.layout.fragment_apple_detail, container, false);

FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
Stopwatch1Fragment stopwatch1Fragment = new Stopwatch1Fragment();
transaction.replace(R.id.fragment_container, stopwatch1Fragment);
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.commit();

return root;
}