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