Aloweiwi Aloweiwi - 1 year ago 126
Android Question

Toast inside a Button OnclickListener is not working

was initialized at the beginning of the code by
and I am trying to make sure that
's value is updated by the value in the edit box by adding a toast when the button is clicked.
but when I run the app, I get the following problem:

Error:(46, 22) error: no suitable method found for makeText(<anonymous OnClickListener>,String,int)
method Toast.makeText(Context,CharSequence,int) is not applicable
(argument mismatch; <anonymous OnClickListener> cannot be converted to Context)
method Toast.makeText(Context,int,int) is not applicable
(argument mismatch; <anonymous OnClickListener> cannot be converted to Context)

This is the part of my code:

protected void onCreate(Bundle savedInstanceState) {

String delay=delayedttxt.getText().toString(); //this will get a string
int intdelay = Integer.parseInt(delay);
}catch(NumberFormatException ex){ // handle your exception

Button btn=(Button)findViewById(;
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(this,"your integer is " + intdelay , Toast.LENGTH_LONG).show();


How can I solve that?

Answer Source

That's because makeText wants to have a Context as a first argument. You are inside the onClick function of an OnClickListener. This means that this points to your OnClickListener.
You must have something like this

Toast.makeText(YourActivity.this,"your integer is " + intdelay , Toast.LENGTH_LONG).show();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download