Toast inside a Button OnclickListener is not working [duplicate]

偶尔善良 提交于 2019-12-02 14:24:39

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

You have given wrong context; change this line:

 Toast.makeText(this,"your integer is " + intdelay , Toast.LENGTH_LONG).show();

to

 Toast.makeText(YourActivity.this,"your integer is " + intdelay , Toast.LENGTH_LONG).show();

Try like this:

Toast.makeText(YourActivityName.this,"your integer is " + intdelay , Toast.LENGTH_LONG).show();

Change the context if not somwthing may wrong in flashLight.switchFlash method

You are trying to show your toast inside the OnClickListener. The this keyword in this case refers to an instance of type OnClickListener, and not Context, like it is required.

You should use <YourActivityClass>.this to refer to the enclosing activity instance, that is a Context and can be use to show the toast.

Please do these in your activity,

Toast.makeText(getApplicationContext(),"your integer is " + intdelay , Toast.LENGTH_LONG).show();

You should change from

Toast.makeText(this,"your integer is " + intdelay , Toast.LENGTH_LONG).show();

to

Toast.makeText(getContext(),"your integer is " + intdelay , Toast.LENGTH_LONG).show();
MurugananthamS

Try inserting this line of code:

 Toast.makeText(getApplicationContext(),"your integer is " + intdelay , Toast.LENGTH_LONG).show();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!