Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class must either be declared abstract or implement abstract method error

This is my code that is suppose to change some text on button press:-

public class MyActivity extends ActionBarActivity {
    TextView txtview;
    Button butto;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        ImageView imageView = (ImageView) findViewById(R.id.image);
        txtview = (TextView) findViewById(R.id.text);
        butto = (Button) findViewById(R.id.buttn);

        butto.setOnClickListener(new View.OnClickListener() {

                public void Onclick(View paramView) {
                txtview.setText("You Clicked it!");
            }
        });

    }}

The View.OnClickListener is underlined and it gives me the error "Class must either be declared abstract or implement abstract method". This code has been mostly copied from the internet and its suppose to be working fine. Probably its a Android Studio error only. How can I get it to work?

like image 783
BrownMonkey300 Avatar asked Apr 22 '15 06:04

BrownMonkey300


People also ask

What happens if abstract method is not implemented?

If abstract class doesn't have any method implementation, its better to use interface because java doesn't support multiple class inheritance. The subclass of abstract class in java must implement all the abstract methods unless the subclass is also an abstract class.

Is it compulsory for a class which is declared as abstract?

An abstract class is not required to have an abstract method in it. But any class that has an abstract method in it or that does not provide an implementation for any abstract methods declared in its superclasses or implemented interfaces must be declared as an abstract class.

Can we implement abstract method in abstract class?

An abstract class is a class that is declared abstract —it may or may not include abstract methods. Abstract classes cannot be instantiated, but they can be subclassed. When an abstract class is subclassed, the subclass usually provides implementations for all of the abstract methods in its parent class.

Is abstract method must be implemented?

An abstract method doesn't have any implementation (method body). A class containing abstract methods should also be abstract. We cannot create objects of an abstract class. To implement features of an abstract class, we inherit subclasses from it and create objects of the subclass.


2 Answers

View.OnClickListener must implement the function onClick() otherwise your class should be abstract, so that you could implement your onClick() function in some child class. But in your case you have made a spelling mistake. It should be onClick() instead of Onclick();

like image 79
Usman Avatar answered Oct 22 '22 17:10

Usman


You got the method name wrong :

public void Onclick(View paramView)

should be

public void onClick(View paramView)

Following Java naming conventions can help you.

like image 30
Eran Avatar answered Oct 22 '22 15:10

Eran