main的未解析引用

main的未解析引用

本文介绍了Kotlin中ID和activity_main的未解析引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实际上是在kotlin中创建一个新应用程序,以在具有格式化信息的框中显示xml文件

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

问题在于,当我构建应用程序时,有一个activity_main,它的ID返回未解析的引用"

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

在这里MainActivity.kt

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

这是activity_main.xml

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>

我不明白该错误,我导入了程序包并在xml中定义了它.我的代码有什么问题?

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

推荐答案

就我而言,删除 import android.R 可以解决此问题.

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

这篇关于Kotlin中ID和activity_main的未解析引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-04 22:01