Android Studio cancelling Toast message for the new message to show -
at first want cancel current showing message in toast new message show search , found need create toast object use .cancel method. create toast object below line of mainactivity error when i'm running application. says "unfortunately, myapp has stopped". confirmed error when declaring toast object below main activity, got commenting out declaration , run without error. , in toast message want cancel current message next message show when want it. because default use toast duration before show next triggered message.
my question why getting error in that? , how can cancel current toast message show new message. in advance!
heres 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(); }
you instantiating new toast object when write line
toast toastobject = toast.maketext(this, "", toast.length_long);
then when call
toastobject.cancel();
you cancelling toast have created, empty.
toast toastobject = toast.maketext(this, "", toast.length_long); <-- new toast creation, set toastobject toastobject.cancel(); <--- cancelling toastobject have created what want keep reference first toast create , cancel that. 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 reference toastobject @ top of class, keep reference when run toastshowmsg method again , cancel appropriate toast.
Comments
Post a Comment