Java 类不是抽象的,不会覆盖 OnClickListener 中的抽象方法 onClick(View)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32467483/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 12:36:40  来源:igfitidea点击:

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

javaandroidandroid-studio

提问by Lincity

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?

我是 android 开发的新手,正在尝试创建一个应用程序来检查给定的字符串是否是回文。这段代码有什么问题?

I am using Android Studio.

我正在使用 Android Studio。

The error is

错误是

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

类不是抽象的,不会覆盖 OnClickListener 中的抽象方法 onClick(View)

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


}

采纳答案by Abdallah Alaraby

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

您只需要覆盖该onClick(View)方法,添加:

@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.OnClickListenerpart without having to override the onClickmethod

但是我可以看到您没有使用此方法,因为您正在使用,setOnClickListener(new View.OnClickListener());因此您可以删除implements View.OnClickListener零件而无需覆盖该onClick方法

回答by Ajeet Kumar Achal

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

回答by live-love

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