Toast message not shown
Asked Answered
L

5

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.

Labuan answered 22/12, 2014 at 11:16 Comment(10)
Change the Context and try again...Impiety
try classname.this instead of getapplicationcontextOlympiaolympiad
Your line of code is correct, instead of using "getApplicationContext()" try using "YourClassName.this"Bassarisk
did you get any error ? be sure you call this line on UI thread, your code is ok problem must be in some where elseGemology
show ur full code pls..Scauper
Please post your codeUnfetter
@Labuan try use ActivityName.this instead of getApplicationContext()Gemology
@shayanpourvatan I've tried that tooo... But got the same result.Labuan
are you sure you get ActivityNotFoundException? try put log statement in code and check thatGemology
@shayanpourvatan I've checked that also, it enters that part of the code. The problem is it shows the toast message on other device.Labuan
O
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

Ostensory answered 22/4, 2015 at 0:5 Comment(4)
This was very easy to miss! What am not certain about though, is that while enabling notifications solved this for the entire app - all activities that is, they were mysteriously working inside of one of the adapters!Gabrielagabriele
Wow! this saves me some hours of stressAdamsun
Thanks, bro! I've disabled the notifications long time ago and I totally forgot about that.Kerchief
2012 - 2019. Bug is still hereShriver
G
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.

Geometry answered 22/12, 2014 at 11:24 Comment(0)
U
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
}
Unfetter answered 22/12, 2014 at 11:19 Comment(2)
same result on using getBaseContext(). I'm using this in a PreferenceActivityLabuan
set ShowToast() outside the dialog. Tell me now it works or notUnfetter
F
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();
Flowerless answered 22/12, 2014 at 11:23 Comment(0)
S
0

Try using getApplicationContext() instead of getBaseContext.

Spirula answered 22/12, 2014 at 12:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.