1

I create a listview in activity_main.xml and name its id "Listem". However, when I switch to MainActivity.kt and use my "My List" file, I get an "Unresolved reference: Listem error". Where do you think the problem originates

enter image description here

activity_main.xml :

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ListView
        android:id="@+id/Listem"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt :

package com.example.burcson

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

class MainActivity : AppCompatActivity() {

    var burclar = arrayOf("Koç","Boğa","İkizler","Yengeç","Aslan","Başak","Terazi","Akrep","Yay","Oğlak","Kova","Balık")

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

        var burclaradapter = ArrayAdapter(this,android.R.layout.simple_list_item_1,burclar)
        Listem.adapter=burclaradapter
    }
}
Zoe
  • 27,060
  • 21
  • 118
  • 148
herik06
  • 158
  • 3
  • 11
  • Does this answer your question? [Unresolved reference for synthetic view when layout is in library module](https://stackoverflow.com/questions/48378696/unresolved-reference-for-synthetic-view-when-layout-is-in-library-module) – Zoe Dec 21 '20 at 13:50
  • I checked but couldn't fix the problem. Thanks anyway.. – herik06 Dec 21 '20 at 13:58
  • Hi @Herik06 you might have the latest Gradle android plugin in your studio , Kotlin-android-extension has been deprecated, you should consider using view binding, checkout blog for detailed explanation and anti-patterns regarding it [viewbinding](https://chetangupta.net/viewbinding/) – Chetan Gupta Dec 24 '20 at 11:41

2 Answers2

0

You need to retrieve the View in some way.

Simplest way:

val listem = findViewById<ListView>(R.id.Listem)

Or a modern approach would be to use the ViewBinding library: https://developer.android.com/topic/libraries/view-binding

zOqvxf
  • 1,469
  • 15
  • 18
0

In other to use

Listem.adapter=burclaradapter

You need to add Kotlin synthetics to your app level build.gradle

apply plugin: `kotlin-android-extensions`

But Kotlin synthetics has been deprecated so use viewBinding instead in your app level build.gradle

android {
    ...
    buildFeatures {
        viewBinding true
    }
}

I hope this helps. Read more from here

Striped
  • 2,544
  • 3
  • 25
  • 31
Kakyire
  • 112
  • 6