Can´t import kotlinx.android.synthetic.main.activity_main.*

Viewed 7682

I´m learning how to program on kotlin and I´m trying to follow a book exercise (Android Programming with Kotlin for Beginners, chapter 12), however, there is a part where I need to add the following import statement:

import kotlinx.android.synthetic.main.activity_main.* 

So I can import my Button and TextView instances from activity_main.xml. But the "kotlinx" word appears on color red, therefore, all the Button and TextView instances I´m trying to use appear red as well.

Not even using Alt + Enter gives me the import option.

I´ll leave a copy of the code I´m using and an image so you can appreciate the errors I have.

package com.example.kotlinmeetui

import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(), View.OnClickListener {

    // An Int property to hold a value
    private var value = 0

    override fun onClick(p0: View?) {
        TODO("Not yet implemented")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Listen for all the button clicks
        btnAdd.setOnClickListener(this)
        btnTake.setOnClickListener(this)
        txtValue.setOnClickListener(this)
        btnGrow.setOnClickListener(this)
        btnShrink.setOnClickListener(this)
        btnReset.setOnClickListener(this)
        btnHide.setOnClickListener(this)
    }
}

enter image description here

3 Answers

You need apply plugin kotlin-android-extensions in app build.gradle module like this

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
}

However, you should do like @Uuu Uuu wrote above.

Related