我正在尝试在我的片段布局中添加一个FrameLayout
,只是为了保留空间以编程方式添加另一个片段。这是代码:
片段 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}
下面是片段类代码:
片段类代码
@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
时,片段类发现TextView
当我在片段的 xml 布局中的FrameLayout
之后创建另一个TextView
并尝试使用它时,它仍然不起作用(片段没有通过它的 ID 找到视图)。所以看起来,只有第一个视图(R.id.detail)在工作,而Fragment类在那之后找不到任何其他添加的视图。请帮忙!
在片段 xml layoug 中将宽度和高度设置为匹配父级,如下所示
<?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;
}
尝试通过以下方式复制onCreateViewcode
@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;
}