0

At first what I want is to cancel the current showing message in Toast for the new message to show so I search and found that I need to create an Toast Object to use the .Cancel method. So I create a toast object just below the line of MainActivity but I get error when i'm running the application. It says "Unfortunately, MyApp has stopped". Confirmed that the error is when declaring the toast object below the main activity, I got that by commenting out the declaration and it run without error. And in toast message what I want is to cancel the current message for the next message to show just when I want it. Because the default is that it use all the Toast duration before it show the next triggered message.

My question is why am I getting an error in that? And how can I cancel the current Toast message to show my new message. Thanks in advance!

heres the code in declaring of Toast object

public class MainActivity extends AppCompatActivity {

    Toast toastObject = Toast.makeText(this, "", Toast.LENGTH_LONG);

my toastShowMsg code:

public void toastShowMsg(String message) {
    Toast toastObject = Toast.makeText(this, "", Toast.LENGTH_LONG);
    toastObject.cancel();
    toastObject = Toast.makeText(this, message, Toast.LENGTH_LONG);
    toastObject.show();
}
Joseph Reyes
  • 69
  • 1
  • 3
  • 10
  • paste this line Toast toastObject = Toast.makeText(this, "", Toast.LENGTH_LONG); withing onCreate() method – Avishek Das Apr 06 '17 at 14:20
  • 2
    Possible duplicate of [How to cancel Toast](http://stackoverflow.com/questions/4395062/how-to-cancel-toast) – Josh Laird Apr 06 '17 at 14:20
  • Try shoulf work : [http://stackoverflow.com/questions/4395062/how-to-cancel-toast](http://stackoverflow.com/questions/4395062/how-to-cancel-toast) – JakSok Apr 06 '17 at 14:20
  • wouldn't it be unable to use outside the onCreate() method? I tried it and yea I can not call it anymore. – Joseph Reyes Apr 06 '17 at 14:28

1 Answers1

0

You are instantiating a new Toast object when you write the line

Toast toastObject = Toast.makeText(this, "", Toast.LENGTH_LONG);

then when you call

toastObject.cancel();

you are cancelling the Toast that you have just created, which is empty.

Toast toastObject = Toast.makeText(this, "", Toast.LENGTH_LONG); <-- new Toast creation, set to toastObject
toastObject.cancel(); <--- cancelling the toastObject that you have just created

What you want to do is keep a reference to that first Toast you create and then cancel that. It would look something like this:

public YourActivity extends AppCompatActivity
{
    Toast toastObject;

    ...


public void toastShowMsg(String message) {
    if (toastObject != null)
        toastObject.cancel();
    toastObject = Toast.makeText(this, message, Toast.LENGTH_LONG);
    toastObject.show();
}

By adding a reference to toastObject at the top of your class, it will keep a reference to it when you run the toastShowMsg method again and will then cancel the appropriate Toast.

Josh Laird
  • 6,974
  • 7
  • 38
  • 69
  • So i put the declaration in the a method ToastShowMsg so every time I want to show message i'll call that. and There's .cancel() in there after the ToastObject declartion. But when running the app it don't cancel the current message and wait for its duration to finish then it shows the new message. – Joseph Reyes Apr 06 '17 at 14:23
  • Can you edit your first post with the `ToastShowMsg` method? – Josh Laird Apr 06 '17 at 14:25
  • Wow this solves my problem. Thank you very much. 1 more thing I just want to know, why am I getting error when declaring this Toast toastObject = Toast.makeText(this, "", Toast.LENGTH_LONG); at the top of my class? Then error's gone when it's just Toast toastObj; ? – Joseph Reyes Apr 06 '17 at 14:40
  • You should read the error logs that appear because they'll help you in the future. When you use `makeText()` at the top this error displays: `Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference`. This is because when you reference `this` in `makeText(this, ...)` you reference the `Activity` which hasn't been initialised yet and therefore `Context` is null. Happy coding :) – Josh Laird Apr 06 '17 at 14:46
  • 1
    Ahh yeeaaah I got it. I realize it hasn't been initialize yet that's why im getting error. Thanks very much for the helps – Joseph Reyes Apr 06 '17 at 14:49