在 BottomSheetDialog 中膨胀 RecyclerView

Inflating RecyclerView inside BottomSheetDialog

我在使用活动在 BottomSheetDialog 中膨胀我的 recyclerView 时遇到问题。

我不断收到错误消息:

java.lang.IllegalStateException: recycler_provider must not be null

我打开对话框的方法是:

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:

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>

谁知道问题出在哪里?

问题是你没有初始化recycler_provider

recycler_provider = layoutInflater.findViewById<RecyclerView>(R.id.your_recyclerview_id)