Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build a Simple Android App (2014) Basic Android Programming Adding the onClick() Method

Benjamin Grider
Benjamin Grider
3,005 Points

OnClickListener error

The line "View.OnClickListener listener = new View.OnClickListener() {" gives an error stating "Class 'Anonymous class derived from OnClickListener' must be either be declared abstract or implement abstract method 'onClick(View)' in 'OnClickListener' ". How do I fix this? Thanks

1 Answer

Daniel Hartin
Daniel Hartin
18,106 Points

Hi Ben

Not sure what you are trying to achieve however if you are just trying to define the View.OnClickListener as a variable to use later you should end the line with a semi-colon (;) by opening up the curly braces it is asking the compiler to treat the statement as a class declaration not a variable like the below

View.OnClickListener listener = new View.OnClickListener();

If you are trying to set the OnClickListener you should use.

yourButtonVariableName.setOnClickListener(new View.OnClickListener(){
            // the onClick method must be included when you declare a new OnClickListener object
            public void onClick(View v) {
            //Your code for when your button was clicked would go here
            }
}

Hope this helps, however if you are still hitting some walls try posting your entire code and I can take a look.

Daniel