logcat error: invoke suspend at findNavController().navigate(navDirections)
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
viewModel.goodInfoStateFlow.collectLatest { goodInfoState ->
when (goodInfoState) {
is GoodInfoState.Success -> {
val goodInfo = goodInfoState.goodInfo
Log.d(TAG, "goodInfo: $goodInfo")
val navDirections =
CargoFragmentDirections.actionCargoFragmentToGoodInfoJourneysFragment(
goodInfo
)
findNavController().navigate(navDirections)
}
is GoodInfoState.Error -> {
AlertDialog.Builder(requireContext())
.setMessage(goodInfoState.message)
.setNegativeButton(
"Cancel"
) { dialogInterface, _ ->
dialogInterface.dismiss()
}
}
// GoodInfoState.Loading -> TODO()
}
}
}
}
Error Message:
androidx.fragment.app.Fragment$InstantiationException: Unable to instantiate fragment .GoodInfoJourneysFragment: calling Fragment constructor caused an exception at androidx.fragment.app.Fragment.instantiate(Fragment.java:633) at androidx.fragment.app.FragmentContainer.instantiate(FragmentContainer.java:57) at androidx.fragment.app.FragmentManager$2.instantiate(FragmentManager.java:448) at androidx.navigation.fragment.FragmentNavigator.instantiateFragment(FragmentNavigator.java:132) at androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.java:162) at androidx.navigation.fragment.FragmentNavigator.navigate(FragmentNavigator.java:58) at androidx.navigation.NavController.navigate(NavController.java:1066) at androidx.navigation.NavController.navigate(NavController.java:944) at androidx.navigation.NavController.navigate(NavController.java:877) at androidx.navigation.NavController.navigate(NavController.java:863) at androidx.navigation.NavController.navigate(NavController.java:851) at $onViewCreated$1$1$1.invokeSuspend(CargoFragment.kt:97) java.lang.NullPointerException at fragment.GoodInfoJourneysFragment.(GoodInfoJourneysFragment.kt:17)
3条答案
按热度按时间jei2mxaa1#
在我更改为-〉private瓦尔绑定get()=之后
并且解决了这个问题。
产地:
修改后:
ego6inou2#
请检查第17行的
GoodInfoJourneysFragment
代码。异常堆栈跟踪中的这一行java.lang.NullPointerException at fragment.GoodInfoJourneysFragment.(GoodInfoJourneysFragment.kt:17)
个表示您尝试使用某个对象,但在使用之前尚未示例化。
11dmarpk3#
新增
并尝试了