我在使用 activites 将我的 recyclerView 膨胀到 BottomSheetDialog 时遇到问题。
我不断收到错误:
java.lang.IllegalStateException:recycler_provider 不能为空
我打开对话框的方法是:
private fun openOptionsDialog(context: Context) {
val dialog = BottomSheetDialog(context)
val layoutInflater: View =
LayoutInflater.from(context).inflate(R.layout.providers_pop_up, null)
dialog.setContentView(layoutInflater)
dialog.show()
recycler_provider.adapter = ProviderAdapter(getProvidersList())
recycler_provider.layoutManager = LinearLayoutManager(this)
recycler_provider?.setHasFixedSize(true)
layoutInflater.close.setOnClickListener {
dialog.dismiss()
}
}
我的适配器类:
class ProviderAdapter(private val items: ArrayList<ProviderModel>) : RecyclerView.Adapter<ProviderAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ProviderAdapter.ViewHolder {
val view: View = LayoutInflater.from(parent.context).inflate(R.layout.provider_item, parent, false)
return ViewHolder(view)
}
override fun getItemCount() = items.size
override fun onBindViewHolder(holder: ProviderAdapter.ViewHolder, position: Int) {
holder.providerTitle.text = items[position].providerName
holder.providerImage.setImageResource(items[position].providerImage)
}
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
val providerTitle: TextView = itemView.providerItemName
val providerImage: ImageView = itemView.providerItemImage
}
以及提供程序项的 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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/providerRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.002">
<ImageView
android:id="@+id/providerItemImage"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:src="@drawable/meo"></ImageView>
<TextView
android:id="@+id/providerItemName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:text="MEO"
android:textSize="21sp">
</TextView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
有谁知道问题出在哪里?