尝试获取 NavController 时出现 NullPointerException
NullPointerException when trying to get NavController
我无法理解错误的原因java.lang.NullPointerException: null cannot be cast to non-null type androidx.navigation.fragment.NavHostFragment
这是我的主机片段:
class HostFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_host, container, false)
// error here
val navHostFragment = requireActivity().supportFragmentManager.findFragmentById(R.id.hostFragment) as NavHostFragment
//
val navController = navHostFragment.navController
root.host_text_view.setOnClickListener {
Log.d("Tag", "clicked")
navController.navigate(R.id.action_hostFragment_to_secondFragment)
}
return root
}
}
我想通过单击 textView 导航到 SecondFragment。然后回来。
和导航图。xml 文件:
<navigation 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:id="@+id/nav_graph"
app:startDestination="@id/hostFragment">
<fragment
android:id="@+id/hostFragment"
android:name="com.example.test.HostFragment"
android:label="fragment_host"
tools:layout="@layout/fragment_host" >
<action
android:id="@+id/action_hostFragment_to_secondFragment"
app:destination="@id/secondFragment" />
</fragment>
<fragment
android:id="@+id/secondFragment"
android:name="com.example.test.SecondFragment"
android:label="fragment_second"
tools:layout="@layout/fragment_second" >
<action
android:id="@+id/action_secondFragment_to_hostFragment"
app:destination="@id/hostFragment" />
</fragment>
</navigation>
导航图的最终形式如下:
您无需找到 navHostFragment 即可导航
val root = inflater.inflate(R.layout.fragment_host, container, false)
root.host_text_view.setOnClickListener {
Log.d("Tag", "clicked")
if (findNavController().currentDestination?.id == hostFragment)
findNavController().navigate(R.id.action_hostFragment_to_secondFragment)
}
您可以检查 Kotlin 方法来导航
https://developer.android.com/guide/navigation/navigation-navigate
使用导航图的最佳方法是在创建视图之后,因为某些视图需要时间来创建,因此您可能会遇到此异常,因为您的视图仍在处理或创建视图中。
为了避免这种情况,您可以使用 OnViewCreated() as
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
navController = Navigation.findNavController(view)
view.imgNumbrLookUp.setOnClickListener {
navController.navigate(R.id.action_homeFragment_to_webActivity)
}
}
我无法理解错误的原因java.lang.NullPointerException: null cannot be cast to non-null type androidx.navigation.fragment.NavHostFragment
这是我的主机片段:
class HostFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val root = inflater.inflate(R.layout.fragment_host, container, false)
// error here
val navHostFragment = requireActivity().supportFragmentManager.findFragmentById(R.id.hostFragment) as NavHostFragment
//
val navController = navHostFragment.navController
root.host_text_view.setOnClickListener {
Log.d("Tag", "clicked")
navController.navigate(R.id.action_hostFragment_to_secondFragment)
}
return root
}
}
我想通过单击 textView 导航到 SecondFragment。然后回来。
和导航图。xml 文件:
<navigation 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:id="@+id/nav_graph"
app:startDestination="@id/hostFragment">
<fragment
android:id="@+id/hostFragment"
android:name="com.example.test.HostFragment"
android:label="fragment_host"
tools:layout="@layout/fragment_host" >
<action
android:id="@+id/action_hostFragment_to_secondFragment"
app:destination="@id/secondFragment" />
</fragment>
<fragment
android:id="@+id/secondFragment"
android:name="com.example.test.SecondFragment"
android:label="fragment_second"
tools:layout="@layout/fragment_second" >
<action
android:id="@+id/action_secondFragment_to_hostFragment"
app:destination="@id/hostFragment" />
</fragment>
</navigation>
导航图的最终形式如下:
您无需找到 navHostFragment 即可导航
val root = inflater.inflate(R.layout.fragment_host, container, false)
root.host_text_view.setOnClickListener {
Log.d("Tag", "clicked")
if (findNavController().currentDestination?.id == hostFragment)
findNavController().navigate(R.id.action_hostFragment_to_secondFragment)
}
您可以检查 Kotlin 方法来导航 https://developer.android.com/guide/navigation/navigation-navigate
使用导航图的最佳方法是在创建视图之后,因为某些视图需要时间来创建,因此您可能会遇到此异常,因为您的视图仍在处理或创建视图中。
为了避免这种情况,您可以使用 OnViewCreated() as
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
navController = Navigation.findNavController(view)
view.imgNumbrLookUp.setOnClickListener {
navController.navigate(R.id.action_homeFragment_to_webActivity)
}
}