Home > Blockchain >  How to get rid of `Unresolved reference: NavController` error in Android-Kotlin?
How to get rid of `Unresolved reference: NavController` error in Android-Kotlin?

Time:09-27

Other than the NavController, I am getting the same error for AppCompatActivity(), layoutInflater, and a few more in the below mentioned code.

Error: Unresolved reference: NavController and so on...

MY Code:


package com.example.wordsapp

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import androidx.navigation.NavHost
import androidx.navigation.ui.setupActionBarWithNavController
import com.example.wordsapp.databinding.ActivityMainBinding

/**
 * Main Activity and entry point for the app. Displays a RecyclerView of letters.
 */
class MainActivity : AppCompatActivity() {
    private lateinit var navController: NavController
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        val binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHost
        navController = navHostFragment.navController
        setupActionBarWithNavController(navController)
    }

    override fun onSupportNavigateUp(): Boolean {
        return navController.navigateUp() || super.onSupportNavigateUp()
    }
}

CodePudding user response:

  1. First check if you can build the project.

if the project can be built, then it may be a Kotlin/JVM mismatch. But if you find a build error, consider checking your dependencies.

if the above solution does not fix the error, try to close android studio completely and then restart it.

CodePudding user response:

To add to Dron's answer - if none of that works you can also try File > invalidate cache's and restart

  • Related