1

I just started Android Development and have hit a snag, installed the latest stable build of Android Studio, using Kotlin, I just for the life of me can not get the MainActivity to recognize any ID from the layout. I've restarted the project, Clean Project, Rebuild Project, you name it. Code is bellow please if anyone can help.

It really is just a HelloWorld app and yet nothing from the layout can be referenced, I started the project again and still nothing from before will reference.

MainActivity code

package com.example.findmyage

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        

    }
}

And for the layout:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Find My Age"
        android:textSize="19sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.144" />

    <EditText
        android:id="@+id/yearBornInput"
        android:layout_width="304dp"
        android:layout_height="48dp"
        android:autofillHints=""
        android:ems="10"
        android:hint="Enter Year Born"
        android:inputType="number"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.497"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView"
        app:layout_constraintVertical_bias="0.05" />

    <TextView
        android:id="@+id/ageResult"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="79dp"
        android:text="Your Age Will Be Here"
        android:textSize="20sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/yearBornInput"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/buttonCalc"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Get My Age"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/ageResult"
        app:layout_constraintVertical_bias="0.267" />

</androidx.constraintlayout.widget.ConstraintLayout>
Zoe
  • 27,060
  • 21
  • 118
  • 148
RawalD
  • 361
  • 2
  • 14
  • Can you add how you are trying to reference your view without any success? – Tayyab Mazhar Dec 25 '20 at 09:17
  • Does this answer your question? https://stackoverflow.com/questions/34169562/unresolved-reference-kotlinx – private static Dec 25 '20 at 09:44
  • So i tried that now, got the error Gradle sync failed: Cannot add task 'clean' as a task with that name already exists. Consult IDE log for more details (Help | Show Log) (373 ms), after which i deleted the line and the gradle ran perfect though i get an error stating that my Android license is not accepted – RawalD Dec 25 '20 at 09:59
  • But now i am able to reference IDs in the layout, though that warning about the Android License not being accepted makes me wonder, surely Android Studio with Kotlin can't be this buggy right ? – RawalD Dec 25 '20 at 10:00
  • And now when trying to run the app it's giving me : Cause: compileSdkVersion is not specified. Please add it to build.gradle , yet it is in the gradle, also it gives me an EDIT CONFIGURATIONS window now when trying to run the app. – RawalD Dec 25 '20 at 10:17

1 Answers1

5

Finally after a while ,somehow despite using the lastest Android Studio it requires the apply plugin: "kotlin-android-extensions" command in the build.gradle(app) file after the plugins{} in the top file.

    id "com.android.application"
    id "kotlin-android"
   }

  apply plugin: "kotlin-android-extensions"
RawalD
  • 361
  • 2
  • 14
  • OK, seems this allows synthetics to work, there are I think depreciated. Think is I cont get the reference to work with View bindings. – Ben Edwards Sep 13 '22 at 17:34