【问题标题】:Object is not abstract and does not implement abstract member public abstract fun onClick(p0: View!): Unit对象不是抽象的,没有实现抽象成员 public abstract fun onClick(p0: View!): Unit
【发布时间】:2020-11-03 20:18:04
【问题描述】:

在过去的一天中,我没有找到任何可以显示如何执行此操作的内容,我所看到的所有内容都带有一个基本按钮,我无法复制该按钮以与图像按钮一起使用。使用 setOnClickListener 似乎根本不起作用,尽管我发现使用它们的唯一案例是 5 岁以上。

在 Android Studio 中是否有相当于链接活动的 Storyboard?

这是我找到的一个例子,但已经 7 岁了。

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val myButton =
            findViewById<View>(R.id.live) as ImageButton

        myButton.setOnClickListener(object : OnClickListener() {
            // When the button is pressed/clicked, it will run the code below
            fun onClick() {
                // Intent is what you use to start another activity
                val intent = Intent(this, LiveActivity::class.java)
                startActivity(intent)
            }
        })
    }
}

给出以下错误:

Object is not abstract and does not implement abstract member public abstract fun onClick(p0: View!): Unit defined in android.view.View.OnClickListener

【问题讨论】:

    标签: android android-layout kotlin android-imagebutton


    【解决方案1】:

    问题是您没有将 View 参数包含到您的 onClick 覆盖中。 OnClickListener.onClick 的签名包含 View(被点击的视图)作为其参数,因此 onClick()(没有参数)与该签名不匹配。

    您可以显式添加它(在这种情况下,您还需要使用 ActivityName.this 显式引用 Activity 的 this,因为 this 否则会引用 OnClickListener):

        myButton.setOnClickListener(object : View.OnClickListener {
            // When the button is pressed/clicked, it will run the code below
            override fun onClick(view: View) {
                // Replace ActivityName with the name of your Activity that this is in
                val intent = Intent(ActivityName.this, LiveActivity::class.java)
                startActivity(intent)
            }
        })
    

    或使用 Kotlin 的 SAM conversions 隐式添加(我会这样做):

        // When the button is pressed/clicked, it will run the code below
        myButton.setOnClickListener { // there's an implicit view parameter as "it"
            // Intent is what you use to start another activity
            val intent = Intent(this, LiveActivity::class.java)
            startActivity(intent)
        }
    

    【讨论】:

    • 好的,第二个工作正常,第一个有原始问题......为了别人的理智,你能解释一下原因吗?以及发生了什么(或在上下文中解释它的文档)还有 Google Image 按钮以启动新活动(或视图)(对于像我这样搜索的人)
    • 编辑澄清,也解决了第一个问题。
    【解决方案2】:

    修复

    更改自:

     myButton.setOnClickListener(object : OnClickListener { })
    

     myButton.setOnClickListener(object : View.OnClickListener { })
    
    

    所以方法是:

    override fun onClick(v: View?) {
       // Do something
    }
    

    代替你的:

    fun onClick() {
    
    }
    

    完整代码:

    class MainActivity : AppCompatActivity() {
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
    
            val myButton = findViewById<ImageButton>(R.id.live)
    
            myButton.setOnClickListener(object : View.OnClickListener {
                // When the button is pressed/clicked, it will run the code below
                override fun onClick(v: View?) {
                    // Intent is what you use to start another activity
                    val intent = Intent(this, LiveActivity::class.java)
                    startActivity(intent)
                }
            })
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2018-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多