Home > database >  setOnClickListener has not been written well, it gives an error
setOnClickListener has not been written well, it gives an error

Time:05-17

I'm trying to rewrite my code from android:onClick9" to setOnClickListener` since that crashes, bu I'm getting an error. Code

@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

enter image description here

EDIT 2 I have tried button9 but still the label of button9 doesn't change and new Button.OnClickListener() is shadowed.

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


        );


        }

CodePudding user response:

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.

You should rename your method or override on click separately.

  • Related