3

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.

prince
  • 1,129
  • 1
  • 15
  • 38

5 Answers5

15

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

randallmeadows
  • 833
  • 1
  • 10
  • 19
4

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.

Shayan Pourvatan
  • 11,898
  • 4
  • 42
  • 63
krunal patel
  • 2,369
  • 1
  • 11
  • 11
2

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
}
Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
0

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

Try using getApplicationContext() instead of getBaseContext.

max59
  • 606
  • 1
  • 7
  • 16