Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用kotlin在菜单中创建可单击链接_Java_Android_Mobile_Kotlin_Navigation - Fatal编程技术网

Java 使用kotlin在菜单中创建可单击链接

Java 使用kotlin在菜单中创建可单击链接,java,android,mobile,kotlin,navigation,Java,Android,Mobile,Kotlin,Navigation,我试着做一个底部导航,我有这个代码 val bottomNavigation = findViewById<View>(R.id.bottom_navigation) as BottomNavigationView bottomNavigation.setOnNavigationItemSelectedListener { item -> when (item.itemId) { R.id.botom__nav__home

我试着做一个底部导航,我有这个代码

val bottomNavigation = findViewById<View>(R.id.bottom_navigation) as 
  BottomNavigationView
    bottomNavigation.setOnNavigationItemSelectedListener { item ->
        when (item.itemId) {

            R.id.botom__nav__home ->
                // Action when tab 1 selected
                val intent = Intent(this, HomeActivity::class.java)
            R.id.botom__nav__profile ->
                // Action when tab 2 selected
                val intent = Intent(this, LikeActivity::class.java)
            else ->
                // Action when tab 3 selected
                val intent = Intent(this, ProfileActivity::class.java)
        }
        true
    }
    startActivityForResult(intent, 99)
}
对于“何时”中的每个元素


有人能帮我修复这些错误吗?

请删除此行中的
项->

bottomNavigation.setOnNavigationItemSelectedListener { item ->
    when (item.itemId) {` 

问题是您在when块内多次声明
val intent
。要解决这个问题,只需将意图声明移到when块之外,例如:

lateinit var intent:Intent
bottomNavigation.setOnNavigationItemSelectedListener { item ->
    when (item.itemId) {

        R.id.botom__nav__home ->
            // Action when tab 1 selected
            intent = Intent(this, HomeActivity::class.java)
        R.id.botom__nav__profile ->
            // Action when tab 2 selected
            intent = Intent(this, LikeActivity::class.java)
        else ->
            // Action when tab 3 selected
            intent = Intent(this, ProfileActivity::class.java)
    }
    true
}
startActivityForResult(intent, 99)

见下面donfuxx的回答。或者将->之后的每个元素用大括号括起来{val intent….}