转到kotlin中菜单项选择的其他活动

转到kotlin中菜单项选择的其他活动,kotlin,menu,Kotlin,Menu,l添加了在我的应用程序工具栏中选择的选项菜单项。我想在点击项目上添加操作,甚至转到其他活动。l意图但不起作用 override fun onOptionsItemSelected(item: MenuItem): { when (item.itemId) { R.id.flightarrbeforbgw -> Intent intent = new Intent(this, FlightsAr

l添加了在我的应用程序工具栏中选择的选项菜单项。我想在点击项目上添加操作,甚至转到其他活动。l
意图
但不起作用

override fun onOptionsItemSelected(item: MenuItem): {

            when (item.itemId) {
                R.id.flightarrbeforbgw ->
                    Intent intent = new Intent(this, FlightsArrivelBeforBGW.class);
                this.startActivity(intent)

                else ->
                    return null
            }
        }

我试过is代码,他的工作很好

  override fun onOptionsItemSelected(item: MenuItem): Boolean {

        val id = item.itemId

        //noinspection SimplifiableIfStatement

        if (id == R.id.searchflights) {

            val intent = Intent(this, FlightsArrivelBeforBGW::class.java)
            this.startActivity(intent)
            return true
        }

        if (id == R.id.flightarrbeforbgw) {
            Toast.makeText(this, "Android Menu is Clicked", Toast.LENGTH_LONG).show()
            return true
        }

        if (id == R.id.flight_dep_list) {
            Toast.makeText(this, "Android Menu is Clicked", Toast.LENGTH_LONG).show()
            return true
        }

        return super.onOptionsItemSelected(item)

    }

您的意图格式不正确。应该是这样的:

Intent (this, YourActivity::class.java)
因此,您的代码应该如下所示:

when (item.itemId) {
    R.id.flightarrbeforbgw ->{
        this.startActivity(Intent(this,FlightsArrivelBeforBGW::class.java))
        return true
    }
    else -> super.onOptionsItemSelected(item)
}

代码的答案是正确的,而且他和我合作得很好,为什么你对我的答案投反对票?