【问题标题】:Kotlin: does not have a NavController setKotlin:没有设置 NavController
【发布时间】:2021-04-17 13:02:09
【问题描述】:

我刚刚在 android studio 中创建了新项目(还没有更改任何一行,完全全新),第一次在模拟器上运行时出现以下错误:

java.lang.IllegalStateException: Activity com.my.app.MainActivity@138b31b does not have a NavController set on 2131230989

Code

Activity

class MainActivity : AppCompatActivity() {

    private lateinit var appBarConfiguration: AppBarConfiguration

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val toolbar: Toolbar = findViewById(R.id.toolbar)
        setSupportActionBar(toolbar)

        val fab: FloatingActionButton = findViewById(R.id.fab)
        fab.setOnClickListener { view ->
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show()
        }
        val drawerLayout: DrawerLayout = findViewById(R.id.drawer_layout)
        val navView: NavigationView = findViewById(R.id.nav_view)
        val navController = findNavController(R.id.nav_host_fragment)
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        appBarConfiguration = AppBarConfiguration(setOf(
                R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow), drawerLayout)
        setupActionBarWithNavController(navController, appBarConfiguration)
        navView.setupWithNavController(navController)
    }

    override fun onCreateOptionsMenu(menu: Menu): Boolean {
        // Inflate the menu; this adds items to the action bar if it is present.
        menuInflater.inflate(R.menu.main, menu)
        return true
    }

    override fun onSupportNavigateUp(): Boolean {
        val navController = findNavController(R.id.nav_host_fragment)
        return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
    }
}

main_content

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/app_bar_main">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>

PS:错误指向:val navController = findNavController(R.id.nav_host_fragment)

有什么想法吗?

【问题讨论】:

  • 这可能是您使用的 Android Studio 模板中的一个错误。我总是使用“空活动”,而不是必须撕掉所有生成的废话。但是,您能否发布整个堆栈跟踪,而不仅仅是错误?
  • @CommonsWare 我找到了用以下代码替换提到的错误行的临时解决方案,它现在似乎可以工作val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment val navController = navHostFragment.navController 可能是你所说的错误。

标签: android kotlin


【解决方案1】:

不久前我遇到了同样的问题。我不确定为什么会发生这种情况,因为您已经设置了内容视图。它适用于除onCreate 之外的任何地方。在onCreate 我不得不像这样找到它:

val navigationHost =
            supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navigationHost.navController

【讨论】:

  • 是的,这正是我修复它所做的(刚刚在上面的 cmets 中提到过:D)
猜你喜欢
  • 1970-01-01
  • 2021-03-18
  • 1970-01-01
  • 2018-11-03
  • 2020-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-24
  • 1970-01-01
相关资源
最近更新 更多