Toast message not shown

限于喜欢 提交于 2019-12-06 10:13:21

问题


I am trying to show a toast message in my application using the following code.

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");
                            Toast.makeText(getBaseContext(), "Activity Not Found", Toast.LENGTH_LONG).show(); // For the context I tried using getBaseContext, ActivityName.this also
                        }
                    }

            });

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

But this message is being shown only on few devices. I've tested this code on HTC One X having Android version 4.2.2 which is working.

The same code if I test on Micromax A63 which is also having Android 4.2.2, but it doesn't work on it.

I've searched over Internet for this kind of errors and they are mostly telling about the app notification disabling option in the settings menu. But my application notification are not disabled.

EDIT

I'm doing it inside an AlertDialog

Can someone help me solve this issue.


回答1:


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




回答2:


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.




回答3:


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
}



回答4:


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



回答5:


Try using getApplicationContext() instead of getBaseContext.



来源:https://stackoverflow.com/questions/27601611/toast-message-not-shown

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