Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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


}
like image 956
Lincity Avatar asked Sep 08 '15 21:09

Lincity


2 Answers

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

like image 190
Abdallah Alaraby Avatar answered Sep 21 '22 04:09

Abdallah Alaraby


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");
        }
    }
);
like image 36
Ajeet Kumar Achal Avatar answered Sep 17 '22 04:09

Ajeet Kumar Achal