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?
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.
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.
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.
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.
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()
;
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With