Home>
kotlin-android-extensions is not working!

Please write your question in detail here.
I added id'kotlin-android-extensions' to build.gradle (app), but kotlin's import kotlinx.android.synthetic.main.activity_main. * Does not appear as a candidate.

Error message

Unresolved reference: hitText

Corresponding source code
package com.examplekanehiro.highandlow
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle


class MainActivity: AppCompatActivity () {
    private val tag = "high and low"
    private var yourCard = 0
    private var droidCard = 0
    private var hitCount = 0
    private var losecount = 0
    private var gameStart = false
    private var answered = false
    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate (savedInstanceState)
        setContentView (R.layout.activity_main)
    }
    override fun onResume () {
        super.onResume ()
        hitCount = 0
        losecount = 0
        hitText.text = getString (R.string.hit_text) ←
↑ The hit text part turns red, and the text part is drawn with a red line.
        loseText.text = getString (R.string.lose_text) ←
 ↑ The same error as above occurs!
    }
}
<? xml version = "1.0" encoding = "utf-8"?><TextView
        android: id = "@ + id/hitText"
        android: layout_width = "wrap_content"
        android: layout_height = "wrap_content"
        android: layout_marginStart = "68dp"
        android: layout_marginTop = "16dp"
        android: text = "@ string/hit_text"
        android: textAppearance = "@ style/TextAppearance.AppCompat.Large"
        app: layout_constraintBottom_toBottomOf = "@ + id/lose_text"
        app: layout_constraintStart_toStartOf = "parent"
        app: layout_constraintTop_toTopOf = "@ + id/lose_text"
        app: layout_constraintVertical_bias = "1.0" /><TextView
        android: id = "@ + id/lose_text"
        android: layout_width = "wrap_content"
        android: layout_height = "wrap_content"
        android: layout_marginTop = "16dp"
        android: layout_marginEnd = "64dp"
        android: text = "@ string/lose_text"android: textAppearance = "@ style/TextAppearance.AppCompat.Large"
        app: layout_constraintEnd_toEndOf = "parent"
        app: layout_constraintTop_toTopOf = "parent" /><Button
        android: id = "@ + id/highBtn"
        android: layout_width = "wrap_content"
        android: layout_height = "wrap_content"
        android: layout_marginStart = "36dp"
        android: layout_marginBottom = "32dp"
        android: text = "@ string/high_text"
        android: textAppearance = "@ style/TextAppearance.AppCompat.Large"
        app: layout_constraintBottom_toBottomOf = "parent"
        app: layout_constraintStart_toStartOf = "parent" /><Button
        android: id = "@ + id/lowBtn"
        android: layout_width = "wrap_content"
        android: layout_height = "wrap_content"
        android: layout_marginEnd = "52dp"
        android: layout_marginBottom = "36dp"
        android: text = "@ string/low_text"
        android: textAppearance = "@ style/TextAppearance.AppCompat.Large"
        app: layout_constraintBottom_toBottomOf = "parent"
        app: layout_constraintEnd_toEndOf = "parent" /><Button
        android: id = "@ + id/nextBtn"
        android: layout_width = "wrap_content"
        android: layout_height = "wrap_content"
        android: layout_marginStart = "8dp"
        android: layout_marginEnd = "8dp"
        android: layout_marginBottom = "92dp"
        android: text = "@ string/next_text"
        android: textAppearance = "@ style/TextAppearance.AppCompat.Large"
        app: layout_constraintBottom_toTopOf = "@ + id/highBtn"
        app: layout_constraintEnd_toEndOf = "parent"
        app: layout_constraintHorizontal_bias = "0.158"
        app: layout_constraintStart_toEndOf = "@ + id/highBtn" /><ImageView
        android: id = "@ + id/youCardImage"
        android: layout_width = "0dp"
        android: layout_height = "210dp"
        android: layout_marginStart = "24dp"
        android: layout_marginTop = "8dp"
        android: layout_marginEnd = "31dp"
        android: layout_marginBottom = "8dp"
        android: src = "@ drawable/z02"
        app: layout_constraintBottom_toTopOf = "@ + id/nextBtn"
        app: layout_constraintEnd_toStartOf = "@ + id/droidCardImage"
        app: layout_constraintStart_toStartOf = "parent"
        app: layout_constraintTop_toTopOf = "parent" /><ImageView
        android: id = "@ + id/droidCardImage"
        android: layout_width = "140dp"
        android: layout_height = "210dp"
        android: layout_marginStart = "48dp"
        android: layout_marginTop = "8dp"
        android: layout_marginEnd = "24dp"
        android: layout_marginBottom = "8dp"
        android: src = "@ drawable/z01"
        app: layout_constraintBottom_toTopOf = "@ + id/nextBtn"app: layout_constraintEnd_toEndOf = "parent"
        app: layout_constraintStart_toEndOf = "@ + id/youCardImage"
        app: layout_constraintTop_toTopOf = "parent" /><TextView
        android: id = "@ + id/resultText"
        android: layout_width = "match_parent"
        android: layout_height = "wrap_content"
        android: layout_marginStart = "16dp"
        android: layout_marginTop = "76dp"
        android: layout_marginEnd = "16dp"
        android: gravity = "center_horizontal"
        android: textAppearance = "@ style/TextAppearance.AppCompat.Large"
        app: layout_constraintEnd_toEndOf = "parent"
        app: layout_constraintStart_toStartOf = "parent"
        app: layout_constraintTop_toTopOf = "parent" /></androidx.constraintlayout.widget.ConstraintLayout>

↓ build.gradle (app)

plugins {
    id'com.android.application'
    id'kotlin-android'
    id'kotlin-android-extensions'
}
android {
    compileSdkVersion 30
    buildToolsVersion "30.0.2"
    defaultConfig {
        applicationId "com.examplekanehiro.highandlow"
        minSdkVersion 26
        targetSdkVersion 30
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile ('proguard-android-optimize.txt'),'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility JavaVersion.VERSION_1_8
        targetCompatibility JavaVersion.VERSION_1_8
    }
    kotlinOptions {
        jvmTarget = '1.8'
    }
}
dependencies {
    implementation "org.jetbrains.kotlin: kotlin-stdlib: $kotlin_version"
    implementation'androidx.core: core-ktx: 1.3.2'
    implementation'androidx.appcompat: appcompat: 1.2.0'
    implementation'com.google.android.material: material: 1.2.1'
    implementation'androidx.constraintlayout: constraintlayout: 2.0.4'
    testImplementation'junit: junit: 4. +'
    androidTestImplementation'androidx.test.ext: junit: 1.1.2'
    androidTestImplementation'androidx.test.espresso: espresso-core: 3.3.0'
What I tried

Introduced id'kotlin-android-extensions' in build.gradle (app).
If i enter the hitText part, import should be added, but even if you enter it many times, there are no candidates for import kotlinx.android.synthetic.main.activity_main. *.

  • Answer # 1

    Did you first add the description to build.gradle and then click the Sync Now hyperlink to sync? If you're not sure, try syncing again by choosing Files → Sync Project with Gradle Files from the menu.

    Then, if you write the ID of the component in the Kotlin source file, it will be in red if there is no import statement, but if you move the mouse cursor to that part, you should see tips showing the solution as shown in the figure below. If you press Alt + Enter as instructed here, the import statement will be inserted and you can solve it.