Attempt to invoke virtual method 'int io.ak1.Bubble.getId()' on a null object reference
heskadon opened this issue · comments
My app implement navigation controller. I got this error when try to navigate to another fragment within navigation graph.
My first MovieFragment has deep link to MovieDetailFragment.
Error log :
java.lang.NullPointerException: Attempt to invoke virtual method 'int io.ak1.Bubble.getId()' on a null object reference
at io.ak1.BubbleTabBar.setSelectedWithId(BubbleTabBar.kt:69)
at com.kadon.moviebasejetpack.MainActivity.onCreate$lambda-1(MainActivity.kt:40)
at com.kadon.moviebasejetpack.MainActivity.lambda$m7AD07XFDrhvcoED8pyMVzjbbRU(Unknown Source:0)
at com.kadon.moviebasejetpack.-$$Lambda$MainActivity$m7AD07XFDrhvcoED8pyMVzjbbRU.onDestinationChanged(Unknown Source:2)
at androidx.navigation.NavController.dispatchOnDestinationChanged(NavController.java:504)
at androidx.navigation.NavController.navigate(NavController.java:1149)
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 com.kadon.moviebasejetpack.ui.movie.MovieFragment.onClickedCharacter(MovieFragment.kt:56)
This is my MainActivity :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val bubble = binding.bubbleTabBar
val navHostFragment = supportFragmentManager.findFragmentById(
R.id.nav_host_fragment_activity_main
) as NavHostFragment
navController = navHostFragment.navController
appBarConfiguration = AppBarConfiguration(navController.graph)
setupActionBarWithNavController(navController, appBarConfiguration)
bubble.addBubbleListener { id ->
bubble.onNavDestinationSelected(id, navController)
}
navController.addOnDestinationChangedListener { _, destination, _ ->
bubble.setSelectedWithId(destination.id, false)
}
}
This HomeFragment list onclick listner :
private fun onClickedCharacter(id: Int) {
findNavController().navigate(
R.id.action_navigation_movie_to_navigation_movie_detail,
bundleOf("id" to id)
)
}
Hello @heskadon
This is crashing because on navigating to detail page
bubble.setSelectedWithId(destination.id, false)
is getting called in addOnDestinationChangedListener
and as there is no view with id R.id.action_navigation_movie_to_navigation_movie_detail in BubbleTabBar viewgroup.
This is resolved in the new version 1.0.8
implementation 'io.ak1:bubbletabbar:1.0.8'
Yes thanks for 1.0.8 and for the io.ak1:bubbletabbar:1.0.7
you can do :
val destinationId: Int =
when (destination.id) {
R.id.navigation_inner_fragment -> {
R.id.navigation_start_fragment
}
......................................
else -> {
destination.id
}
}