I'm having following error when I'm calling toast("Toast's message text") from Android Fragment:
java.lang.NoSuchMethodError: No virtual method getActivity()Landroid/app/Activity; in class Landroid/support/v4/app/Fragment; or its super classes (declaration of 'android.support.v4.app.Fragment' appears in name-of-the-file-classes.dex)
I'm using Anko v0.9.1 and Kotlin 1.0.6
What might be the cause of this crash? Standard Android Toast works just fine. Also toast() function works inside Activities.
This is how you can show an Android Toast message from a Fragment: Toast. makeText(getActivity(), "Click!", Toast. LENGTH_SHORT).
A toast provides simple feedback about an operation in a small popup. It only fills the amount of space required for the message and the current activity remains visible and interactive. Toasts automatically disappear after a timeout.
It is very strange, probably it is a bug.
But next works for me
activity.toast("Toast's message text")
or
context.toast("Toast's message text")
If you want to use it inside a class (an adapter, for example), surely you have to get the activity first (context)
holder.itemView.setOnClickListener(
{ view ->
view.getContext().toast("Message")
}
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With