5

I learning kotlin, and now for me not cearly some moment. I have xml

<ImageView
                    android:id="@+id/aries"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:tag="1"
                    android:onClick="clickItemHoro"
                    android:src="@drawable/aries" />

and fragment

class ChooseYourHoroscope : Fragment(){

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        val view = inflater.inflate(R.layout.fragment_welcome_old, container, false)

        return view;
    }

    fun clickItemHoro(v: View?){
        Log.e("clickItemHoro", v!!.tag.toString())
    }

}

when i click button i have error:

Could not find method clickItemHoro(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatImageView with id 'aries'

why it happen? Code vary simple, but it not work, i cant understand why

g71132
  • 671
  • 2
  • 10
  • 17
  • 3
    *Kotlin in xml onClick not work* ... it would not work in Java, too ... *in a parent or ancestor Context* ... is Fragment a Context? – Selvin Nov 30 '16 at 10:07
  • i understood, tanks! – g71132 Nov 30 '16 at 10:15
  • 1
    [http://stackoverflow.com/questions/14139774/android-app-crashing-fragment-and-xml-onclick](http://stackoverflow.com/questions/14139774/android-app-crashing-fragment-and-xml-onclick) ref this link said. – Jacob Dec 05 '16 at 01:55

5 Answers5

1

Try to use Kotlin Extensions plugin, it should work.

in build.gradle (app) add apply plugin: 'kotlin-android-extensions'

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

then import it in MainActivity.kt

import kotlinx.android.synthetic.main.activity_main.*
Dian
  • 470
  • 1
  • 6
  • 20
  • 1
    I still get this problem with the latest version of everything as of 3-6-18, but I don't see it in Android Studio -- I see it when building with ./gradlew on the command line. I have the plugins, the import, the Manifest.xml tools:context line, all of it. The weird thing is I still get an .apk in my output folder even though lint is telling me it failed. There's some disconnect perhaps in the lint realm? Since lint doesn't really happen when building in Android Studio... – Bungles Mar 07 '18 at 02:16
1

apply plugin: 'kotlin-android-extensions' must use kotlin extenstion Then it will work

Tarun konda
  • 1,740
  • 1
  • 11
  • 19
1

As mentioned above it's a good idea to add the Kotlin extensions, additionally you could set an onClickListener on the ImageView directly in your code instead of declaring the method in your xml files: onClick:

aries.setOnClickListener { doSomething() }

Jonas H
  • 81
  • 1
  • 3
0

In Gradle file add

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'

then activity add

   import kotlinx.android.synthetic.main.activity_main.*
sasikumar
  • 12,540
  • 3
  • 28
  • 48
  • I still get this problem with the latest version of everything as of 3-6-18, but I don't see it in Android Studio -- I see it when building with ./gradlew on the command line. I have the plugins, the import, the Manifest.xml tools:context line, all of it. The weird thing is I still get an .apk in my output folder even though lint is telling me it failed. There's some disconnect perhaps in the lint realm? Since lint doesn't really happen when building in Android Studio... – Bungles Mar 07 '18 at 02:16
0

Based on the above answers and comments posting the full solution here so that it would be helpful for others.

First remove android:onClick attribute from the xml code of ImageView.

Then change the fragment code as below:

class ChooseYourHoroscope : Fragment(){
  override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                          savedInstanceState: Bundle?): View? {
      val view = inflater.inflate(R.layout.fragment_welcome_old, container, false)
      val aries = view.findViewById<ImageView>(R.id.aries)
      aries.setOnClickListener{
          //Write ur action here
      }

      return view;
   }
}
Vinu
  • 166
  • 2
  • 10