Search code examples
androidbuttonclick

setOnClickListener for a button


Just getting started with Android development and I can't figure out why this won't work. Here is the error that I am getting (on the last line):

The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (MainActivity)

And here is the code. Seems pretty simple, but I don't see what the problem is. Can anyone help? Thank you!

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_myActivity);

        View continue = findViewById(R.id.ContinueBtn);
        continue.setOnClickListener(this);

    }
}

Solution

  • Try this.

    public class MainActivity extends Activity implements OnClickListener
    

    When you pass this object into setOnClickListener then you need to implement OnClickListenere .