'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 ?



Solution 1:[1]

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

Solution 2:[2]

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

Solution 3:[3]

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'
}

Solution 4:[4]

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

Solution 5:[5]

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.

Solution 6:[6]

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()
        }
    }
}

Solution 7:[7]

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.

Solution 8:[8]

Change this line

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

to this

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

you are missing R reference.

Solution 9:[9]

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

Solution 10:[10]

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.

Solution 11:[11]

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

import com.package.name.R