Object is not abstract and does not implement abstract member public abstract fun onClick(p0: View!): Unit
Asked Answered
W

2

7

I haven't found anything over the past day that shows how to do this action, everything I've seen is with a basic button of which I am unable to replicate for use with an image button. using setOnClickListener does not seem to work at all though the only cases I found of using them were 5+ years old.

Is there a Storyboard equivalent of linking activities in Android Studio?

Here is an example I found but 7 years old.

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)
            }
        })
    }
}

gives the following error:

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

post Listener

Winery answered 3/11, 2020 at 20:18 Comment(0)
T
6

The problem is that you haven't included the View parameter to your onClick override. The signature of OnClickListener.onClick includes a View (the View that was clicked) as its parameter, so onClick() (with no parameters) doesn't match that signature.

You could either add it explicitly (in which case you also need to refer to the Activity's this explicitly with ActivityName.this, as this refers to the OnClickListener otherwise):

    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)
        }
    })

or use Kotlin's SAM conversions to add it implicitly (I'd do this approach):

    // 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)
    }
Thaumaturge answered 4/11, 2020 at 0:32 Comment(2)
ok the second one works as is the first has the original problem... for sake of others sanity, could you possibly explain why. and what's going on (or documentation that explains it in context) Also Google Image button to launch new activity (or view) (for those searching like me)Winery
Edited to clarify, fixed the issues with the first one as well.Thaumaturge
C
1

Fix

Change from:

 myButton.setOnClickListener(object : OnClickListener { })

to

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

so method will be:

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

instead yours:

fun onClick() {

}

Full code:

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)
            }
        })
    }
}
Cerallua answered 4/11, 2020 at 0:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.