简体   繁体   中英

Kotlin Error at runtime - Unresolved reference Object ID

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

在此处输入图像描述

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM