Answer the question
In order to leave comments, you need to log in
Why doesn't setOnclickListener work?
stopped workingsetOnclickListener
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal">
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_margin="30dp">
<TextView
android:layout_width="wrap_content"
android:text="TextView"
android:layout_height="wrap_content"
android:id="@+id/tvOut"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="50dp">
</TextView>
<Button
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="@+id/btnOk"
android:text="OK"
android:layout_width="100dp">
</Button>
<Button
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="@+id/btnCancel"
android:text="Cancel"
android:layout_width="100dp">
</Button>
</LinearLayout>
</LinearLayout>
package com.example.pakk
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btnOk.setOnClickListener
}
}
Answer the question
In order to leave comments, you need to log in
It doesn't know what btnOk is because you didn't declare such a variable anywhere.
val btnOk = findViewById<Button>(R.id.btnOk)
If there is no such code in another project, and everything works, then Kotlin android extensions are used there. In build.gradle you need to include:
apply plugin: 'kotlin-android-extensions'
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question