我试着做一个底部导航,我有这个代码
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)
}
我有以下错误:
' Expecting an expression '
' Expecting "->" '
对于“When”中的每个元素...
有人能帮我修复这些错误吗?
发布于 2018-06-04 01:50:50
问题是,您在when块中多次声明val intent
。要解决这个问题,只需将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)
发布于 2018-06-04 01:45:04
请删除此行中的item ->
bottomNavigation.setOnNavigationItemSelectedListener { item ->
when (item.itemId) {`
https://stackoverflow.com/questions/50669071
复制相似问题