【发布时间】:2021-11-28 22:36:50
【问题描述】:
我正在尝试在 RecyclerView 中创建一个 CardView,但我无法启动它,因为应用程序在出现此错误“RecyclerView 没有 LayoutManager”后崩溃。我已尝试应用其他帖子建议的有关同一问题的修复,但到目前为止没有运气。
提前谢谢你。
HomeActivity.kt
class HomeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
// Retrieves data from source
val arrayList = ArrayList<PreviewCardModel>()
arrayList.add(PreviewCardModel("Product A","500mg / 1000mg", R.drawable.image, 500))
arrayList.add(PreviewCardModel("Product D","Sample", R.drawable.image, 5040))
arrayList.add(PreviewCardModel("Product E","Sample", R.drawable.image, 5500))
arrayList.add(PreviewCardModel("Product F","Sample", R.drawable.image, 2500))
val myAdapter = CardAdapter(arrayList, this)
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL,false)
recyclerView.adapter = myAdapter
CardAdapter.kt
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.sample.appname.R
import com.sample.appname.model.PreviewCardModel
import kotlinx.android.synthetic.main.preview_card.view.*
class CardAdapter (private val arrayList: ArrayList<PreviewCardModel>, val context: Context) :
RecyclerView.Adapter<CardAdapter.CardViewHolder>() {
class CardViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
fun bindItems (model: PreviewCardModel) {
itemView.previewName.text = model.title
itemView.previewDesc.text = model.desc
itemView.previewPrice.text = model.price.toString()
itemView.previewImage.setImageResource(model.image)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CardViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.preview_card, parent, false)
return CardViewHolder(view)
}
// Returns size of data list
override fun getItemCount(): Int {
return arrayList.size
}
// Displays data at a certain position
override fun onBindViewHolder(holder: CardViewHolder, position: Int) {
holder.bindItems(arrayList[position])
}
}
activity_home.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"
android:padding="16dp"
tools:context=".HomeActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="@color/browser_actions_title_color"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:orientation="horizontal"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
这是我遇到的错误。
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.sample.appname, PID: 5333
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample.appname/com.sample.appname.HomeActivity}: android.view.InflateException: Binary XML file line #54 in com.sample.appname:layout/activity_home: RecyclerView has no LayoutManager androidx.recyclerview.widget.RecyclerView{b2c2d71 VFED..... ......I. 0,0-0,0 #7f09016f app:id/recyclerView}, adapter:null, layout:null, context:com.sample.appname.HomeActivity@5dcb507
preview_card.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="120dp"
android:padding="10sp"
app:cardCornerRadius="3dp"
app:cardElevation="3dp"
app:cardUseCompatPadding="true">
<ImageView
android:id="@+id/previewImage"
android:layout_width="80dp"
android:layout_height="80dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:src="@drawable/heart"
android:contentDescription="@string/product_image" />
<TextView
android:id="@+id/previewName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/product_name"
android:textSize="11sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/previewImage" />
<TextView
android:id="@+id/previewDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SAMPLE"
android:textSize="8sp"
android:textColor="@color/colorPrimary"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/previewName"
tools:ignore="SmallSp" />
<TextView
android:id="@+id/previewPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="@string/samplePrice"
android:textColor="@color/colorPrimary"
android:textSize="11sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />
</androidx.cardview.widget.CardView>
【问题讨论】:
-
尝试从
RecyclerView中删除android:orientation="horizontal" -
@Zain 仍然没有运气。它显示相同的错误。
-
您是否尝试将
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"添加到 xml 中?只是为了检查。顺便说一句,根据您的错误消息,适配器为空。 -
我也会尝试用经典的
findById替换 kotlin-synthetic。 -
当布局 XML 中的
<RecyclerView>在其中包含子元素时会发生该特定错误,这是您无法做到的。您发布的布局在<RecyclerView>中显然没有子项,因此问题可能出在其他地方。你确定这是当前和准确的布局吗?您确定这是当前正确的例外吗?您确定您正在运行当前版本吗?你可能有多个activity_home布局吗?例如,纵向和横向各一个,或者针对不同的 Android 版本等?
标签: android xml kotlin android-layout android-recyclerview