【问题标题】:Make clickable link in menu with kotlin使用 kotlin 在菜单中制作可点击的链接
【发布时间】:2018-06-03 17:23:59
【问题描述】:

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

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

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

【问题讨论】:

  • 请参阅下面的 donfuxx 回答。或者将 -> 之后的每个元素括在花括号 {val intent....}

标签: java android mobile kotlin navigation


【解决方案1】:

问题是您在 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)

【讨论】:

    【解决方案2】:

    请删除此行中的item -&gt;

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

    【讨论】:

      猜你喜欢
      • 2015-10-06
      • 1970-01-01
      • 2013-08-21
      • 1970-01-01
      • 2016-12-12
      • 2010-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多