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)
CodePudding user response:
After I change to -> private val binding get() =
And solved this problem.
Origin:
class CargoFragment : Fragment() {
private var _binding: FragmentCargoBinding? = null
private val binding = _binding!!
}
After modify:
class CargoFragment : Fragment() {
private var _binding: FragmentCargoBinding? = null
private val binding get() = _binding!!
}
CodePudding user response:
Kindly check your GoodInfoJourneysFragment
's code on line 17. The line in the exception stack trace
java.lang.NullPointerException at fragment.GoodInfoJourneysFragment.(GoodInfoJourneysFragment.kt:17)
says that you have some object that you try to use, but it hasn't been instantiated before using.