【问题标题】:Opening a fragment from another fragment within same bottom navigation activity in Android [duplicate]在Android的同一底部导航活动中从另一个片段打开一个片段[重复]
【发布时间】:2021-02-17 15:02:05
【问题描述】:

我正在尝试用底部导航活动中的另一个片段替换一个片段。我想通过单击片段中的按钮来实现这一点。

但我收到以下错误:

java.lang.IllegalArgumentException: No view found for id 0x7f090074 for fragment

这是我的替换功能:

private fun replaceFragment(fragment: Fragment) {
        val fragmentManager = childFragmentManager
        fragmentManager.commit {
            setReorderingAllowed(true)
            replace(R.id.content, fragment)
        }
    }

底部导航的 XML 代码:

<androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true">

            <fragment
                android:id="@+id/home_nav_host"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                app:defaultNavHost="true"
                app:layout_constraintBottom_toTopOf="@+id/home_bottom_nav"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:navGraph="@navigation/home_nav" />

            <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/home_bottom_nav"
                android:layout_width="match_parent"
                android:layout_height="66dp"
                android:background="#fff"
                android:textAlignment="center"
                app:itemIconTint="@drawable/home_bottom_nav_colour_selector"
                app:itemTextColor="@drawable/home_bottom_nav_colour_selector"
                app:labelVisibilityMode="labeled"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:menu="@menu/bottom_nav_menu" />
        </androidx.constraintlayout.widget.ConstraintLayout>

FirstFragment 将被 SecondFragment 替换,FirstFragment 当前由 BottomNavigation 显示

FirstFragment()代码:

class ExpenseFragment : Fragment() {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

button.setOnClickListener {
            fragment = SecondFragment()
            replaceFragment(fragment)
        }

private fun replaceFragment(fragment: Fragment) {
        val fragmentManager = childFragmentManager
        fragmentManager.commit {
            setReorderingAllowed(true)
            replace(R.id.content, fragment)
        }
    }
}

我已经尝试了所有类似问题的解决方案,但我的问题仍然没有解决

【问题讨论】:

  • 你试过this吗?
  • 在你建议的方法中,container_id 应该是什么?它是我用于底部导航的同一个容器吗?
  • 在您的底部导航活动中,您有容器,这是显示片段的视图。它通常是 xml 中的 FrameLayout 或 Fragment。请粘贴您的 xml 和 java 文件代码,并在问题中显示您尝试过的内容。
  • 这是我的 xml 和 kotlin 代码
  • 您的 xml 中带有 id "home_nav_host" 的片段标签是您的容器。用这个片段ID替换你的replaceFragment函数中的R.id.content

标签: java android kotlin android-fragments fragmentmanager


【解决方案1】:

理想情况下,您应该使用活动类来替换活动中的片段。

fragmentManager.commit {
    setReorderingAllowed(true)
    // Replace whatever is in the fragment_container view with this fragment
    replace<ExampleFragment>(R.id.fragment_container)
}

将此添加到活动类的函数中,并使用片段中的接口或活动对象从片段中调用。

【讨论】:

  • 但我们从不创建活动对象
  • 在我的活动中,我有一个 BottomNav,而我正在尝试从 BottomNav 中的一个片段打开一个片段
  • (-1) 从同一个活动中替换一个活动中的片段是理想的,这并没有写在哪里。片段可以在一个活动中被其他片段替换。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-05-26
  • 1970-01-01
  • 1970-01-01
  • 2021-12-28
  • 2022-01-06
  • 1970-01-01
  • 2019-08-20
相关资源
最近更新 更多