error: Type mismatch: inferred type is Unit but Boolean was expected

error: Type mismatch: inferred type is Unit but Boolean was expected

我在导航时遇到错误 drawer.My 片段上的抽屉 (center_fragment) 无法正常工作!!

  navigationView.setNavigationItemSelectedListener {
            drawerLayout.closeDrawers()

            when (it.itemId) {

                R.id.contact -> findNavController().navigate(
                    centerFragmentDirections.actionCenterFragmentToContactFragment()

                )

                R.id.about_us -> findNavController().navigate(
                    centerFragmentDirections.actionCenterFragmentToAboutFragment()

                )


                else -> true
            }

setNavigationItemSelectedListener 期望您 return 一个布尔值,但您没有 return 在您的 when 分支中的 2 个布尔值:

        navView.setNavigationItemSelectedListener {
            when (it.itemId) {
                R.id.contact -> {
                    findNavController().navigate(centerFragmentDirections.actionCenterFragmentToContactFragment())
                    return@setNavigationItemSelectedListener true
                }
                R.id.about_us -> {
                    findNavController().navigate(centerFragmentDirections.actionCenterFragmentToAboutFragment())
                    return@setNavigationItemSelectedListener true
                }
                else -> true
            }
        }