N
N
Nadir Parilaev2020-10-24 20:49:10
Android
Nadir Parilaev, 2020-10-24 20:49:10

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

    }
}

5cef54e87780.pnghere is a screenshot. Android studio doesn't understand this button or view text.
the most interesting thing is that everything worked on another project. And even more surprising is that if you switch between projects, it works in one project and not in the other.
I can't figure it out for a few days. I did a complete reinstallation of the studio,
please help

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2020-10-24
@Mossobek

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 question

Ask a Question

731 491 924 answers to any question