Java 类必须声明为抽象或实现抽象方法错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29789057/
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 08:36:59  来源:igfitidea点击:

Class must either be declared abstract or implement abstract method error

javaandroidclassandroid-studio

提问by BrownMonkey300

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.OnClickListeneris 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?

View.OnClickListener被强调,并给我的错误“类必须被声明为抽象或实现抽象方法”。这段代码大部分是从互联网上复制的,它应该可以正常工作。可能只是 Android Studio 错误。我怎样才能让它工作?

采纳答案by Usman

View.OnClickListenermust 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();

View.OnClickListener必须实现该函数,onClick()否则您的类应该是抽象的,以便您可以onClick()在某个子类中实现您的函数。但是在您的情况下,您犯了拼写错误。它应该onClick()代替Onclick();

回答by Eran

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.

遵循 Java 命名约定可以帮助您。

回答by SilentKnight

Tricks: add @Overrideabove onClick(View). Without @Override, the warning means you didn't implement onClickListener.

技巧:在@Override上面添加onClick(View)。没有@Override,警告意味着您没有实施onClickListener.