简体   繁体   English

setOnClickListener 没写好,报错

[英]setOnClickListener has not been written well, it gives an error

I'm trying to rewrite my code from android:onClick9" to setOnClickListener` since that crashes, bu I'm getting an error. Code我正在尝试将我的代码从android:onClick9" to setOnClickListener`,因为它崩溃了,但是我遇到了一个错误。代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button button9=findViewById(R.id.button9);

    button9.setOnClickListener(
            new Button.OnClickListener() {
                public void onClick9(View view) {
                button9.setText("OK");
                }
            }
    );
}

**ERROR**
error: <anonymous com.example.tablelayout6.MainActivity$1> is not
abstract and does not override abstract method onClick(View) 
in OnClickListener new Button.OnClickListener() {

EDIT编辑

在此处输入图像描述

EDIT 2 I have tried button9 but still the label of button9 doesn't change and new Button.OnClickListener() is shadowed.编辑 2我已经尝试过button9但 button9 的标签仍然没有改变,并且new Button.OnClickListener()被遮住了。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button9=findViewById(R.id.button9);

        button9.setOnClickListener(
                new Button.OnClickListener() {
                    public void onClick(View view) {
                        TextView status=findViewById(R.id.button9);

                    button9.setText("OK");
                    }
                }


        );


        }

When you pass an anonymous class to method, you have to implements all of its abstract/interface inherited method.当您将匿名类传递给方法时,您必须实现所有抽象/接口继承方法。

In your code, you wrote new method called onClick9, and didn't override the abstract method onClick as needed, so it gives new an error.在您的代码中,您编写了名为 onClick9 的新方法,并且没有根据需要覆盖抽象方法 onClick,因此它给 new 一个错误。

You should rename your method or override on click separately.您应该重命名您的方法或单独单击覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM