Search code examples
androidkotlinandroid-studioreference

How to solve the errors of "Unresolved reference"?


In my first app using Android Studio and Kotlin I still get errors for Unresolved reference It's the only 6 errors left.

How could I fix this? I followed a sample guide to place the files so far.

activity_main.xml is in C:\Users\D 11\AndroidStudioProjects\positionapp\app\src\main\res\layout\

starting lines:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <ToggleButton
        android:id="@+id/toggleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textOn="Ein"
        android:textOff="Aus" />
    <EditText
        android:id="@+id/editText_dt"
        android:layout_width="match_parent"
        android:layout_height="59dp"
        android:hint="dt in Minuten oder Sekunden" />
    <EditText
        android:id="@+id/editText_tnr"
        android:layout_width="match_parent"
        android:layout_height="61dp"
        android:hint="tnr" />
...

MainActivity.kt is in C:\Users\D 11\AndroidStudioProjects\positionapp\app\src\main\java\com\example\positionapp\

lines 36 to 46

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

    locationViewModel = ViewModelProvider(this).get(LocationViewModel::class.
java
)

    toggleButton = findViewById(R.id.toggleButton)
    editTextDt = findViewById(R.id.editText_dt)
    editTextTnr = findViewById(R.id.editText_tnr)
    editTextCdz = findViewById(R.id.editText_cdz)
    scrollableText = findViewById(R.id.scrollableText)
...

Android Studio output

...
> Task :app:compileDebugKotlin FAILED
e: file:///C:/Users/D%2011/AndroidStudioProjects/positionapp/app/src/main/java/com/example/positionapp/MainActivity.kt:38:26 Unresolved reference 'layout'.
e: file:///C:/Users/D%2011/AndroidStudioProjects/positionapp/app/src/main/java/com/example/positionapp/MainActivity.kt:42:39 Unresolved reference 'id'.
e: file:///C:/Users/D%2011/AndroidStudioProjects/positionapp/app/src/main/java/com/example/positionapp/MainActivity.kt:43:37 Unresolved reference 'id'.
e: file:///C:/Users/D%2011/AndroidStudioProjects/positionapp/app/src/main/java/com/example/positionapp/MainActivity.kt:44:38 Unresolved reference 'id'.
e: file:///C:/Users/D%2011/AndroidStudioProjects/positionapp/app/src/main/java/com/example/positionapp/MainActivity.kt:45:38 Unresolved reference 'id'.
e: file:///C:/Users/D%2011/AndroidStudioProjects/positionapp/app/src/main/java/com/example/positionapp/MainActivity.kt:46:41 Unresolved reference 'id'.
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugKotlin'.
> A failure occurred while executing org.jetbrains.kotlin.compilerRunner.GradleCompilerRunnerWithWorkers$GradleKotlinCompilerWorkAction

I find my suggestions to use Clean and Rebuild Project, the latter of the two isn't in my version of Android Studio, and I can't find the option to re-add it in the modifying menu for the tool bars. How to select this option?


Solution

  • In my only Kotlin file MainActivity.kt the line

    package com.example.positionapp

    at the start was missing.

    In the AndroidManifest.xml file there was missing

    package="com.example.smspositionsenderandreceiver"
    

    within the <manifest ... tag.