【问题标题】:Android studio onmenuitemclicklistener function gives unexpected type specificationAndroid studio onmenuitemclicklistener 函数给出了意想不到的类型规范
【发布时间】:2021-10-04 07:52:39
【问题描述】:

我目前正在尝试使用 kotlin 在我的 android studio 项目中实现一个按钮。我对kotlin不是很熟悉,所以参考这个教程:https://www.geeksforgeeks.org/popup-menu-in-android-with-example/

我想在我的 android 应用程序中创建一个弹出菜单。

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

       navigateToMapFragmentIfNeeded(intent)
       // mapbox
        Mapbox.getInstance(this, getString(R.string.mapbox_access_token));
        setContentView(R.layout.activity_main)
        /* Referencing and Initializing the button */
        var popbutton: Button = findViewById<View>(R.id.clickBtn) as Button

        // Setting onClick behavior to the button
        popbutton.setOnClickListener(View.OnClickListener { // Initializing the popup menu and 
        //giving the reference as current context
            val popupMenu = PopupMenu(this@MainActivity, popbutton)

            // Inflating popup menu from popup_menu.xml file
            popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu())


            // problem line below
            popupMenu.setOnMenuItemClickListener(PopupMenu: OnMenuItemClickListener(){
                fun onMenuItemClick(menuItem: MenuItem): Boolean {
                    // Toast message on menu item clicked
                    Toast.makeText(
                        this@MainActivity,
                        "You Clicked " + menuItem.getTitle(),
                        Toast.LENGTH_SHORT
                    ).show()
                    return true
                }
            })
            // Showing the popup menu
            popupMenu.show()
        })
        // Request permission
        requestPermission()
    }

当我尝试在我的模拟器中运行时,我在上面的代码中将这些错误注释为“下面的问题行”:
“意外的类型规范”
“期待,”
“期待一个表达”

谁能告诉我如何解决这个问题?

感谢任何和所有帮助!谢谢

【问题讨论】:

    标签: android android-studio kotlin popup


    【解决方案1】:

    去掉setOnMenuItemClickListener最后一行最后的)

        popupMenu.setOnMenuItemClickListener(PopupMenu: OnMenuItemClickListener(){
            fun onMenuItemClick(menuItem: MenuItem): Boolean {
                // Toast message on menu item clicked
                Toast.makeText(
                    this@MainActivity,
                    "You Clicked " + menuItem.getTitle(),
                    Toast.LENGTH_SHORT
                ).show()
                return true
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2014-06-22
      • 1970-01-01
      • 2015-07-13
      • 1970-01-01
      • 1970-01-01
      • 2021-12-13
      • 2014-05-24
      • 2017-10-26
      • 1970-01-01
      相关资源
      最近更新 更多