gpt4 book ai didi

java - Android:无法添加/替换 fragment

转载 作者:行者123 更新时间:2023-12-01 18:16:25 25 4
gpt4 key购买 nike

我正在尝试向我的应用程序添加一个新 fragment 。
但这个 fragment 是空的。

我做错了什么?

fragment 启动:

ConvertOptions fragment = (ConvertOptions) getSupportFragmentManager()
.findFragmentByTag(Utils.CONVERT_FRAGMENT_TAG);

if (fragment != null)
{
getSupportFragmentManager()
.beginTransaction()
.add(R.id.container, fragment, Utils.CONVERT_FRAGMENT_TAG)
.commit();
}

Fragment类的代码:

    public class ConvertOptions extends Fragment
{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.convert_options_fragment, null);

return view;
}
}

XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/options_convert_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">


<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button2"
android:layout_gravity="center_horizontal" />

最佳答案

只需这样做:

ConvertOptions mConvertOptions = new ConvertOptions();
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.content_frame, mConvertOptions);
fragmentTransaction.commit();

关于java - Android:无法添加/替换 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29251083/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com