Error with Nav Controller

Asked

Viewed 21 times

0

I am facing problems running my app because it does not open the main, the problem is in my Nav controler but do not know how to solve it.

Log:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.pokedex, PID: 19404
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pokedex/com.example.pokedex.ui.main.MainActivity}: java.lang.IllegalStateException: Activity com.example.pokedex.ui.main.MainActivity@17e9680 does not have a NavController set on 2131230964
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:169)
    at android.app.ActivityThread.main(ActivityThread.java:6521)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: java.lang.IllegalStateException: Activity com.example.pokedex.ui.main.MainActivity@17e9680 does not have a NavController set on 2131230964
    at androidx.navigation.Navigation.findNavController(Navigation.java:61)
    at androidx.navigation.ActivityKt.findNavController(Activity.kt:30)
    at com.example.pokedex.ui.main.MainActivity.configNavDrawer(MainActivity.kt:69)
    at com.example.pokedex.ui.main.MainActivity.onCreate(MainActivity.kt:46)
    at android.app.Activity.performCreate(Activity.java:7051)
    at android.app.Activity.performCreate(Activity.java:7042)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:169) 
    at android.app.ActivityThread.main(ActivityThread.java:6521) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

Main Activity:

class MainActivity : AppCompatActivity() {

private lateinit var toggle: ActionBarDrawerToggle
var currentPath: String? = null
val TAKE_PICTURE = 1
val SELECT_PICTURE = 2

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.main_activity)
    configBottomNavegacao()
    configNavDrawer()

    

 main_activity_bottom_navigation.setupWithNavController
   (pokemons_activity_nav_host.findNavController())

    toggle = ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close )
    drawerLayout.addDrawerListener(toggle)
    toggle.syncState()

    supportActionBar?.setDisplayHomeAsUpEnabled(true)

    navView.setNavigationItemSelectedListener {
        when(it.itemId){
            R.id.drawer_camera -> dispatchCameraIntent()
            R.id.drawer_fav -> dispatchFavoritosIntent()
        }
        true
    }
}

private fun dispatchFavoritosIntent() {
}

private fun configNavDrawer() {
    val navController = findNavController(R.id.main_activity_bottom_navigation)
    findViewById<NavigationView>(R.id.navView)
        .setupWithNavController(navController)

    // verifica onde vai ou nao mostrar o drawer
    navController.addOnDestinationChangedListener { controller, destination, arguments ->
        when(destination.id){
            R.id.listaPokemons -> mostraNavDrawer()
            R.id.listaFavPokemons -> mostraNavDrawer()
            R.id.search -> mostraNavDrawer()
            R.id.detalhesPokemons -> mostraNavDrawer()
        else -> ocultaNavDrawer()
        }
    }
}

private fun mostraNavDrawer() {
    navView.visibility = View.VISIBLE
}
private fun ocultaNavDrawer(){
    navView.visibility = View.GONE
}

private fun configBottomNavegacao() {
    val navHostFragment = supportFragmentManager
        .findFragmentById(R.id.pokemons_activity_nav_host) as NavHostFragment
    val navController = navHostFragment.navController

    // verifica onde vai ou nao mostrar o bottom navigation
    navController.addOnDestinationChangedListener { controller, destination, arguments ->
        when(destination.id){
            R.id.listaPokemons -> mostraBottomNav()
            R.id.listaFavPokemons -> mostraBottomNav()
            R.id.search -> mostraBottomNav()
            R.id.detalhesPokemons -> mostraBottomNav()
            else -> ocultaBottomNavigation()
        }

    }

}

//oculta o botao de navegaçao
private fun ocultaBottomNavigation() {
    main_activity_bottom_navigation.visibility = View.GONE
}

// exibe o botao de navegaçao
private fun mostraBottomNav() {
    main_activity_bottom_navigation.visibility = View.VISIBLE
}

Activity main:

<androidx.drawerlayout.widget.DrawerLayout 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/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.main.MainActivity">

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">


</androidx.constraintlayout.widget.ConstraintLayout>

<androidx.fragment.app.FragmentContainerView
    android:id="@+id/pokemons_activity_nav_host"
    class="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:defaultNavHost="true"
    app:navGraph="@navigation/nav_graph" />

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/main_activity_bottom_navigation"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        android:background="@color/bottomNavigation"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/botao_nav_menu" />


</androidx.constraintlayout.widget.ConstraintLayout>

<com.google.android.material.navigation.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:id="@+id/navView"
    app:headerLayout="@layout/nav_header"
    app:menu="@menu/nav_drawer_menu"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"/>

</androidx.drawerlayout.widget.DrawerLayout>

1 answer

0


You need to call the method findNavController passing the view ID NavHostFragment. That is, in the method configNavDrawer, instead of:

val navController = findNavController(R.id.main_activity_bottom_navigation)

place:

val navController = findNavController(R.id.pokemons_activity_nav_host)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.