views:

353

answers:

4

old question: "Why does creating a Toast crash my application?"

My application runs fine if I don't use toasts but if I want to create and show a simple Toast like this:

   Toast SimpleToast = Toast.makeText(getApplicationContext(), "Just a toast.",Toast.LENGTH_LONG);
   SimpleToast.setGravity(Gravity.TOP, 0, 0);
   SimpleToast.show();

I get these lines in the log and then the app crashes:

NotificationService   enqueueToast pkg=games.MyAppName callback=android.app.ITransientNotification$Stub$Proxy@49603368 duration=1
ResourceType          No package identifier when getting name for resource number 0x00000000
AndroidRuntime        Shutting down VM
dalvikvm              threadid=3: thread exiting with uncaught exception (group=0x4001b188)
AndroidRuntime        Uncaught handler: thread main exiting due to uncaught exception
AndroidRuntime        java.lang.NullPointerException

I've checked and double checked the code above is what makes it crash. I'm running on the emulator as I currently don't have an android phone.

A: 

It seems that you have tried to dereference a Null pointer somewhere, try stepping in the debugger and see where exactly. It could be the .setGravity call. That would indicate that Toast.makeText has failed to create your toast. You should note that toasts can be created and displayed from an Activity or Service.

Nikola Smiljanić
I'm creating the toast inside a class that extends Activity in an OnCLickListener void onClick
Mervin
It seems that I get the error whenever I call Toast.show() outside of the onCreate() method , although I'm 1000% sure that Toast.show() doesn't get called before onCreate() is done. How can I fix this? , I want to show the toast from within an onclick void from an onclicklistener defined in this activity.
Mervin
A: 

Wrap the code in a try-catch and check the stack trace on the Exception that's thrown. It sounds like a callback method on an Activity is being called when you weren't expecting it to.

Richard Szalay
please read the updated question
Mervin
+1  A: 

To the best of my knowledge, you can't show a toast with an application context, you have to show it with an Activity context. The toast isn't bound to your app, it's bound to the activity that you're showing it on. Instead of using getApplicationContext(), try using a variable named ctx containing a reference to your activity.

Good luck!

mattbasta
The documentation says that context is usually your Application or Activity object.
Nikola Smiljanić
Your code alone works fine if place in the `onCreate()` method of a vanilla Activity - so maybe from where you're calling it, `getApplicationContext()` returns null or something (you may want to test return values of each call you make for debugging). I'd agree with mattbasta - store a reference to your Activity in some variable and use that.
Joubarc
It seems that I get the error whenever I call Toast.show() outside of the onCreate() method , although I'm 1000% sure that Toast.show() doesn't get called before onCreate() is done. How can I fix this? , I want to show the toast from within an onclick void from an onclicklistener defined in this activity.
Mervin
@Mervin: Your `OnClickListener` already has access to your `Activity` -- if your Activity is named `Foo`, try `Foo.this` as your `Context`. Never use `getApplicationContext()` for anything involving the UI.
CommonsWare
@CommonsWare: Please read the update part in my post, I'm now creating the toast in onCreate() , but even using Toast.Show() in an other place than onCreate() causes the crash.
Mervin
@Mervin: Please read my comment. I do not care where you are creating the `Toast`. Do not use `getApplicationContext()`. Here are a list of projects that show the successful use of a `Toast` in various places within an application: http://github.com/commonsguy/cw-android/tree/master/Messages/Message/ http://github.com/commonsguy/cw-android/tree/master/Maps/NooYawk/ http://github.com/commonsguy/cw-android/tree/master/Menus/Inflation/ http://github.com/commonsguy/cw-android/tree/master/Threads/Asyncer/
CommonsWare
.. sigh can we stay on the topic of Toast.Show not doing what it's supposed to, if I place the code in oncreate everything works, if I use getapplicationcontext or not as soon as I try to use Toast.show() outside of onCreate I get an error.
Mervin
@Mervin: Sugar attracts more flies than vinegar. If you want an answer, try being a little less abrasive. You're the one that asked for help.
mattbasta
A: 

For some reason, getApplicationContext returns null. Try "ActivityName.this" instead, on your onClick() method, inside the Listener.

Leaudro