AndroidDev
AndroidDev

Reputation: 21247

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);

    }
}

Upvotes: 1

Views: 768

Answers (3)

Ram kiran Pachigolla
Ram kiran Pachigolla

Reputation: 21201

first of all change the continue to some other name as it is the keyword you can't give a keyword as variable name

implements OnClickListener for your Mainactivity

Button continuea = (Button)findViewById(R.id.ContinueBtn);

Upvotes: 0

RajaReddy PolamReddy
RajaReddy PolamReddy

Reputation: 22493

you have to do like this

public class MainActivity extends Activity implements OnClickListener {
   /// code
}

Upvotes: 0

Chirag
Chirag

Reputation: 56935

Try this.

public class MainActivity extends Activity implements OnClickListener

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

Upvotes: 3

Related Questions