我正在使用Jetpack Navigation来处理Fragments的导航.
我一直在关注文档并安装了所需的组件,当尝试显示托管NavHost片段的活动时,应用程序仍然崩溃
我一直在关注文档并安装了所需的组件,当尝试显示托管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还在预览中吗?任何帮助将非常感激..