Issue
By any chance anyone knows why I may be getting an error in my MainActivity file when I try to call a button; I already gave it an id in the activity_main file, can anyone help me shed some light on this please, id in activity_main is "btnDatePicker"
MainActivity file
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnDatePicker.setOnClickListener{
}
}
}
activity_main file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:background="@color/backgroundColor"
tools:context=".MainActivity"
android:padding="16dp">
<Button
android:id="@+id/btnDatePicker"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#929292"
android:text="@string/select_date"
android:layout_marginTop="15dp"
android:textStyle="bold"
android:textSize="20sp"/>
</LinearLayout>
Solution
First thing you need to do is,
Integrating Kotlin Android Extensions in your code
Steps1: Add this to your app.gradle
plugins {
id 'com.android.application'
id 'kotlin-android'
id 'kotlin-android-extensions'
id 'kotlin-kapt'
}
Step2: Add this the import session of your activity
import kotlinx.android.synthetic.main.activity_main.*
Finished. Everything will go well. Please do add your error in the Question to understand more.
Answered By - Varun Chandran
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.