Kotlin 无法扩充布局
Kotlin Cannot Inflate Layout
我正在构建一个 GridView
的应用程序,其中包含动态数量的 CardView
。我在从我创建的自定义适配器访问单个项目布局中的 TextView
元素时遇到问题。
tvCastleName
和 tvShieldTime
是未解析的引用,应用程序将无法编译。
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">
<TextView
android:id="@+id/tvNickname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:text="Nickname"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="24sp"
app:layout_constraintTop_toTopOf="parent" />
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/tvNickname"
android:horizontalSpacing="6dp"
android:verticalSpacing="6dp"
android:numColumns="3"
android:id="@+id/gvCastles" />
items.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center"
android:layout_margin="5dp"
app:cardElevation="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvCastleName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:fontFamily="sans-serif-medium"
android:text="Castle Name"
android:gravity="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/tvCastleName"
android:text="15:00"
android:id="@+id/tvShieldTime"
android:gravity="center" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
GridAdapter.kt
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
class GridAdapter(var nameList: ArrayList<Castle>, var context: Context?) : BaseAdapter() {
override fun getCount(): Int = nameList.size
override fun getItem(position: Int): Castle = nameList[position]
override fun getItemId(position: Int): Long {
TODO("Not yet implemented")
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val nameList = this.nameList[position]
var inflater = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val castleView = inflater.inflate(R.layout.items, null)
castleView.tvCastleName.text = nameList.name!!
castleView.tvShieldTime.text = "15:00"
return castleView
}
}
您必须获得 Unresolved Reference
,因为您用来访问 view
的方法 (Kotlin Synthetics
) 现在是 deprecated
。您可以使用 ViewBinding
访问您的 views
.
您必须对 getItem
方法进行一些更改,以使其与 ViewBinding
一起使用
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val nameList = this.nameList[position]
val itemsBinding = ItemsBinding.inflate(LayoutInflater.from(parent.context), parent, false)
itemsBinding.tvCastleName.text = nameList.name!!
itemsBinding.tvShieldTime.text = "15:00"
return itemsBinding.root
}
我正在构建一个 GridView
的应用程序,其中包含动态数量的 CardView
。我在从我创建的自定义适配器访问单个项目布局中的 TextView
元素时遇到问题。
tvCastleName
和 tvShieldTime
是未解析的引用,应用程序将无法编译。
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">
<TextView
android:id="@+id/tvNickname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-medium"
android:text="Nickname"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="24sp"
app:layout_constraintTop_toTopOf="parent" />
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/tvNickname"
android:horizontalSpacing="6dp"
android:verticalSpacing="6dp"
android:numColumns="3"
android:id="@+id/gvCastles" />
items.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="120dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_gravity="center"
android:layout_margin="5dp"
app:cardElevation="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvCastleName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:fontFamily="sans-serif-medium"
android:text="Castle Name"
android:gravity="center"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@+id/tvCastleName"
android:text="15:00"
android:id="@+id/tvShieldTime"
android:gravity="center" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
GridAdapter.kt
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
class GridAdapter(var nameList: ArrayList<Castle>, var context: Context?) : BaseAdapter() {
override fun getCount(): Int = nameList.size
override fun getItem(position: Int): Castle = nameList[position]
override fun getItemId(position: Int): Long {
TODO("Not yet implemented")
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val nameList = this.nameList[position]
var inflater = context!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val castleView = inflater.inflate(R.layout.items, null)
castleView.tvCastleName.text = nameList.name!!
castleView.tvShieldTime.text = "15:00"
return castleView
}
}
您必须获得 Unresolved Reference
,因为您用来访问 view
的方法 (Kotlin Synthetics
) 现在是 deprecated
。您可以使用 ViewBinding
访问您的 views
.
您必须对 getItem
方法进行一些更改,以使其与 ViewBinding
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
val nameList = this.nameList[position]
val itemsBinding = ItemsBinding.inflate(LayoutInflater.from(parent.context), parent, false)
itemsBinding.tvCastleName.text = nameList.name!!
itemsBinding.tvShieldTime.text = "15:00"
return itemsBinding.root
}