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….}