我正在使用 Jetpack Navigation 来处理 Fragments 的导航。
我一直在关注文档并安装了所需的组件,但在尝试显示托管 NavHost 片段的活动时,应用程序仍然崩溃
例外:
java.lang.IllegalArgumentException: Fragment NavHostFragment{820022f} is not an active fragment of FragmentManager FragmentManager{5a5703c in HostCallbacks{a0b41c5}}
at android.support.v4.app.FragmentManagerImpl.setPrimaryNavigationFragment(FragmentManager.java:3389)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:783)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2595)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2382)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2337)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2244)
主要活动布局
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity" >
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/my_nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
app:navGraph="@navigation/nav_graph"
app:defaultNavHost="true"
/>
</FrameLayout>
主要活动 - Kotlin
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.login_activity)
if (savedInstanceState == null) {
supportFragmentManager.beginTransaction()
.replace(R.id.container, LoginFragment.newInstance())
.commitNow()
}
}
override fun onSupportNavigateUp()
= findNavController(R.id.my_nav_host_fragment).navigateUp()
}
我正在尝试测试 JetPack 功能,而我目前被困在这个功能上,有人知道可能出了什么问题吗?是因为 Android 3.2 仍在预览中吗?任何帮助将非常感激..