admin管理员组

文章数量:1318335

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=";
    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 .jetbrains.kotlinpilerRunner.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?

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/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 .jetbrains.kotlinpilerRunner.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?

Share Improve this question asked Jan 22 at 16:15 Bill2022Bill2022 399 bronze badges 3
  • Please edit the question to also provide the import statements in MainActivity. – tyg Commented Jan 22 at 16:34
  • Perhaps you've defined something else as R instead of using the class android.R? – k314159 Commented Jan 22 at 17:02
  • 1 a possible problem is you import the wrong package, or its a gradle incompatibility, some times happens with Android Studio, you need to make sure you are using the right Android Gradle version with the right Android Studio version – shotmeinthehead Commented Jan 22 at 17:22
Add a comment  | 

1 Answer 1

Reset to default 0

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.

本文标签: androidHow to solve the errors of quotUnresolved referencequotStack Overflow