Android Fragments 在片段中设置片段

qmelpv7a  于 2022-12-27  发布在  Android
关注(0)|答案(1)|浏览(205)

我在这里看到过类似的问题,但都没有帮助。
我有MainActivity和几个片段,它们工作正常,但我想将一个片段设置在另一个片段中。

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val bottomNavigationView = findViewById<BottomNavigationView>(R.id.bottomMenu)
        val hostFragment = supportFragmentManager.findFragmentById(R.id.fragmentContainer) as NavHostFragment
        val navigationController = hostFragment.navController

        bottomNavigationView.setupWithNavController(navigationController)
        setupActionBarWithNavController(navigationController)
    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.fragmentContainer)
        return navController.navigateUp() || super.onSupportNavigateUp()
    }
}
    • 父片段:**
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_main, container, false)
        incomeButton = view.findViewById(R.id.addIncomeButton)
        expenseButton = view.findViewById(R.id.addExpenseButton)
        val childFragment: Fragment = ChartFragment()
        val transaction: FragmentTransaction = childFragmentManager.beginTransaction()
        transaction.replace(R.id.childFragment, childFragment).commit()
        return view
    }
    • 子片段:**
override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        // Inflate the layout for this fragment
        return ComposeView(requireContext()).apply {
            setContent {
                Text(
                text = "here is another important code",
                fontSize = 28.sp
                )
            }
        }
    • 活动的布局:**
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomMenu"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:menu="@menu/bottom_menu" />

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/fragmentContainer"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="475dp"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@+id/bottomMenu"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/nav_map" />
</androidx.constraintlayout.widget.ConstraintLayout>
    • 片段的布局:**
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".fragments.MainFragment">

    <Button
        android:id="@+id/addIncomeButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="21dp"
        android:layout_marginBottom="21dp"
        android:backgroundTint="@color/primary"
        android:text="@string/addIncomeButton"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <Button
        android:id="@+id/addExpenseButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="21dp"
        android:layout_marginBottom="10dp"
        android:backgroundTint="@color/red"
        android:text="@string/addExpenseButton"
        app:layout_constraintBottom_toTopOf="@+id/addIncomeButton"
        app:layout_constraintEnd_toEndOf="parent" />

    <FrameLayout
        android:id="@+id/forChart"
        android:layout_width="match_parent"
        android:layout_height="350dp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

大多数相同问题的解决方案都说要使用childFragmentManager,我也是,但它不起作用。
我得到一个错误java.lang.IllegalArgumentException: No view found for id 0x7f08023f

我想我不明白一些事情,但我不能意识到什么。

∮谢谢你∮

72qzrwbm

72qzrwbm1#

看起来您混淆了事务容器的id-a错误也是这么说的。您试图将ChartFragment放入id为R.id.childFragment的容器中:

transaction.replace(R.id.childFragment, childFragment).commit()

从我在MainFragment布局中看到的内容来看,容器ID是R.id.forChart

transaction.replace(R.id.forChart, childFragment).commit()

相关问题