9

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.

Jan Slominski
  • 2,968
  • 4
  • 35
  • 61

2 Answers2

11

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")
Deni Erdyneev
  • 1,779
  • 18
  • 26
1

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")
                }
        )
Smaillns
  • 2,540
  • 1
  • 28
  • 40