Lincity
Lincity

Reputation: 185

Class is not abstract and does not override abstract method onClick(View) in OnClickListener

I am new to android development and am trying to create an app to check if a given string is Palindrome or not. What is the problem with this code?

I am using Android Studio.

The error is

Class is not abstract and does not override abstract method onClick(View) in OnClickListener

package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.lang.String;
import java.lang.StringBuffer;

public class Alaukik extends AppCompatActivity implements View.OnClickListener

{
    private View v;
    final TextView textView = (TextView) findViewById(R.id.textView2);
    final EditText editText = (EditText) findViewById(R.id.editText);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_alaukik);

        final Button checkbutton = (Button) findViewById(R.id.button);
        checkbutton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                String str = editText.getText().toString();
                StringBuffer b = new StringBuffer(str);
                String reved = b.reverse().toString();
                if (reved.equals(str)) {
                    textView.setText("Palindrome");
                } else {
                    textView.setText("Not a Palindrome");

                }
            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_alaukik, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


}

Upvotes: 7

Views: 27803

Answers (3)

live-love
live-love

Reputation: 52444

If you don't want to make the class abstract, you have to remove the abstract methods that it contains:

public class MyActivity extends AppCompatActivity  {

    protected abstract int myMethod(); --> remove this line

Upvotes: 0

Ajeet Kumar Achal
Ajeet Kumar Achal

Reputation: 11

mybutton.setOnClickListener(
    new Button.OnClickListener () {
        @Override // to override the method
        public void onClick(View view) {
            TextView mytext = (TextView) findViewById(R.id.mytext);
            mytext.setText("clicked now");
        }
    }
);

Upvotes: 1

Abdallah Alaraby
Abdallah Alaraby

Reputation: 2249

You just need to override the onClick(View) method, add:

@Override
public void onClick(View view) {
}

But I can see you're not using this method anyway since you're using setOnClickListener(new View.OnClickListener()); so you can just remove the implements View.OnClickListener part without having to override the onClick method

Upvotes: 12

Related Questions