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

时间:2018-06-03 17:24:00

标签: 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 ->
                // 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”中的每个元素......

有人可以帮我解决这些错误吗?

2 个答案:

答案 0 :(得分:1)

问题是你在when块中多次声明val intent。要解决此问题,只需在when块之外移动intent的声明,例如:

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)

答案 1 :(得分:0)

请删除此行中的private List<Product> _previousGuesses = new List<Product>();

item ->