I am trying to display a toast when this button is pressed. But the code is not working

久未见 提交于 2019-11-28 01:20:46

You have to pass the current Context as first parameter (instead of getBaseContext()). This, in your case, is MainActivity.this.

Toast.makeText(MainActivity.this,"Entered"+userid+"and password entered is"+pass,Toast.LENGTH_SHORT).show();

It is because the getBaseContext() at that point in the code is referencing the click listener. What you want to reference is your activity. You should change the reference of your Context in the Toast message to be View.getContext()(if working on the context from within a subview) or this.

Toast.makeText(getApplicationContext(),"Entered"+userid+"and password entered is"+pass,Toast.LENGTH_SHORT).show();

OR

Toast.makeText(MainActivity.this,"Entered"+userid+"and password entered is"+pass,Toast.LENGTH_SHORT).show();

Method Syntax

public static Toast makeText (Context context, CharSequence text, int duration);

The context to use. Usually your Application or Activity object.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!