button.setOnClickListener. error

后端 未结 2 982
情深已故
情深已故 2021-01-26 07:11

This is my code:

final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v)         


        
2条回答
  •  有刺的猬
    2021-01-26 07:56

    Your code has to be in a method, like onCreate.

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                // Perform action on click
            }
        });
    }
    

提交回复
热议问题