我实际上是在 kotlin 中创建一个新的应用程序,以在带有格式化信息的框中显示一个 xml 文件
问题是当我构建应用程序时,有一个 activity_main,返回“未解决的引用”的 id
Unresolved reference: id
Unresolved reference: id
这里是 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
<?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中定义。我的代码有什么问题?
原文由 Hugo Sohm 发布,翻译遵循 CC BY-SA 4.0 许可协议
在我的例子中,删除
import android.R
解决了这个问题。