0

I am starting to learn android development using Kotlin. I am trying to call object ID in my MainActivity.kt but I am getting an error Unresolved reference : btnDatePicker

Given below is the code

<Button
    android:id="@+id/btnDatePicker"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="15dp"
    android:background="#0E0B0B"
    android:text="SELECT DATE"
    android:textColor="#C5BBBB"
    android:textSize="20sp"
    android:textStyle="bold"
    app:backgroundTint="#186C64" />

Given below is the code in MainActivity.kt

class MainActivity : AppCompatActivity() {

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

    btnDatePicker.setOnClickListener { Toast.makeText(this, "works", Toast.LENGTH_LONG).show() }


    }
}

On hovering over btnDatePicker I see the below options but not sure how to proceed

enter image description here

Kevin Nash
  • 1,511
  • 3
  • 18
  • 37
  • It's not defined, you should get a reference by using findViewById, kotlin synthetic, jetpack view binding... If it's all ok and kotlin synthetic doesn't find it after being configured on gradle try performing a clean and rebuild on your project – jeprubio Dec 30 '20 at 21:59
  • 2
    Does this answer your question? [Unresolved reference: kotlinx](https://stackoverflow.com/questions/34169562/unresolved-reference-kotlinx) – jeprubio Dec 30 '20 at 22:08

1 Answers1

1

Looks like you're trying to use synthetic view binding. Your project needs to have the kotlin-android-extension plugin enabled in build.gradle like:

apply plugin: 'kotlin-android-extensions'

After that you should be able to import your btnDatePicker.

Note that kotlin-android-extensions and synthetic view binding is deprecated so you are probably better off with Jetpack view binding. See https://developer.android.com/topic/libraries/view-binding/migration

laalto
  • 150,114
  • 66
  • 286
  • 303