Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
890 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...