简体   繁体   中英

Unresolved reference in kotlin with id and activity_main

I'm actually creating a new app in kotlin to display an xml file in boxes with the informations formatted

To problem is that when I'm building the app, there is the activity_main, the id that return "Unresolved reference"

Unresolved reference: id
Unresolved reference: id

Here the MainActivity.kt

package com.example.instantsystem

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import java.io.IOException

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val listView = findViewById<ListView>(id.listView)
        var employees: List<Employee>? = null
        try {
            val parser = XmlPullParserHandler()
            val istream = assets.open("employees.xml")
            employees = parser.parse(istream)

            val adapter = ArrayAdapter(this, R.layout.simple_list_item_1, employees)
            listView.adapter = adapter

        } catch (e: IOException) {
            e.printStackTrace()
        }
    }
}

Here the activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.instantsystem.MainActivity">

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

    </ListView>

</android.support.constraint.ConstraintLayout>

I don't understand the error, I imported the package and defined it in xml. What is wrong in my code?

In my case removing import android.R solved the issue.

remove import android.R from imports which might be imported automatically.

On newest verison kotlin-android-extensions needed to apply inside build.gradle (Module..)

plugins {
    id 'com.android.application'
    id 'kotlin-android'
    id 'kotlin-android-extensions'
}

Same as @SkorpEN has mentioned , For me also removing import android.R solved the issue.

The reason why import android.R added from nowhere is, if you paste a java code into Kotlin it will convert to Kotlin automatically and some times this import android.R will also be added automatically.

In my case, I accidentally deleted the package declaration, so adding package your.package.name fixed it for me.

Using Kotlin Android Extensions, you don't need to call findViewById() anymore. Just like that:

import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var employees: List<Employee>? = null
        try {
            val parser = XmlPullParserHandler()
            val istream = assets.open("employees.xml")
            employees = parser.parse(istream)

            val adapter = ArrayAdapter(this, R.layout.simple_list_item_1, employees)
            listView.adapter = adapter  // here, the listView variable name refers to the id given in the xml file

        } catch (e: IOException) {
            e.printStackTrace()
        }
    }
}

Deleting import Android.R will work most of the times as mentioned by SkorpEN .

If you could not find that import, try checking the package name on top of the Activity. If there is no package name add the package name or if the name is already there try deleting the line and rewriting the name.

Change this line

val listView = findViewById<ListView>(id.listView)

to this

val listView = findViewById<ListView>(R.id.listView)

you are missing R reference.

It happened to me after trying to rename my application name and packages and folders. Make sure you search the code for all old names and replace them with the new one.

In my case; i edited the MainActivity.kt codes within automatic imports so I had to add:

import com.package.name.R 

there was no import android.R in my code to remove and I had already declared my package in MainActivity.kt

Updating Android Gradle Plugin worked for me

After failing on Rebuild Project and Make Project, finally Clean Project works for me. (Remember to add the plugin, kotlin-android-extensions beforehand.)

This will happen if the AndroidManifest.xml has the wrong package name.

Since none of the answers above worked for me, I found another solution that did work: prepend the package name to R .

For example, change from this:

onView(withId(R.id.llDirectionsButtonImageView)).perform(click())

To this:

onView(withId(com.myco.appsdk.R.id.llDirectionsButtonImageView)).perform(click())

In my case, the problem was happening in Espresso test code in a multi-module project. One module, appsdk, contains all of the UI code and resource files. Another module, app, creates an Application based on the appsdk and runs Espresso tests on that application. For this reason, I think the Espresso test code in the app module needs to refer to the resource ids in the appsdk with a fully-qualified name. For years, I didn't need to add the prefix. I think a recent dependency upgrade may have made it a requirement to add the prefix.

Here are some relevant version numbers of dependencies/environment I'm using:

  • Android Studio Chipmunk | 2021.2.1
  • macOS 12.4
  • classpath 'com.android.tools.build:gradle:7.2.0'
  • classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.6.10"
  • androidTestImplementation "androidx.test.espresso:espresso-contrib:3.3.0"
  • androidTestImplementation "androidx.test.espresso:espresso-core:3.3.0"
  • implementation "androidx.test.espresso:espresso-core:3.3.0"
  • androidTestImplementation "androidx.appcompat:appcompat:1.4.1"

I know it is older post, but in my case the solution was to import:

* import kotlinx.android.synthetic.main.your_activity. **

It is imported automaticaly if you write a part of your id of textview and select from list which is shown from android studio

be sure you have this plugin mentioned above

plugins { id 'com.android.application' id 'kotlin-android' id 'kotlin-android-extensions' }

After insert it, make the sync action.

After trying all the answers in this post, none of them worked for me, but I was able to fix it and it was very easy.

First I invalidated the cache, then I added this line

import com.your.package.R;

In my case I did not have any import with R, so I had to add it, for a moment I thought it was not a good solution but after reviewing and testing there was no problem, I hope you can solve your problem.

The version of Android Studio Electric Eel

macOS Ventura 13.2

Gradle 7.2.2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM