Toast message not shown

大城市里の小女人 提交于 2019-12-04 15:02:40

In case you haven't figured this out yet, make sure you haven't disabled notifications for the application in question; this also disables toasts.

https://code.google.com/p/android/issues/detail?id=35013

krunal patel

If you are using it in an activity then use:

Toast.makeText(ActivityName.this, "My Toast Message", Toast.LENGTH_SHORT).show();

And if You are using it for fragments then:

Toast.makeText(getActivity, "My Toast Message", Toast.LENGTH_SHORT).show();

OR in Adapter

Toast.makeText(context, "My Toast Message", Toast.LENGTH_SHORT).show();

Note: here in adapter the context means the context you declared in your adapter.

IntelliJ Amiya

Try this

Toast.makeText(getBaseContext(), "My Toast Message", Toast.LENGTH_SHORT).show();

OR

Toast.makeText(PreferenceActivity.this, "My Toast Message", Toast.LENGTH_SHORT).show(); `

For more details .Check THIS

    AlertDialog.Builder alert = new AlertDialog.Builder(this);
            alert.setTitle("Do you want to continue?");
            alert.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    try{
                        //This code generates an Activity Not Found exception   
                        }
                        catch(ActivityNotFoundException e) {
                            System.out.println("Activity Not Found Exception Raised");
                           ShowToast();
                        }
                    }

            });

            alert.setNegativeButton("Cancel",
                    new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {
                }
            });
            alert.show();

}

public void ShowToast()
{
 Toast.makeText(getBaseContext(), "Activity Not Found", Toast.LENGTH_LONG).show(); // For the context I tried using getBaseContext, ActivityName.this also
}

Context context;

1.Then call and initialize on OnCreate()

context=this; (Use in Activity)

context=this.getActivity(); (Use in Fragment)

Then use

Toast.makeText(context, "My Toast Message", Toast.LENGTH_LONG).show();

Try using getApplicationContext() instead of getBaseContext.

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