在BottomSheetDialog内部充气RecyclerView

问题描述 投票:0回答:1

我在使用活动工具在BottomSheetDialog内膨胀recyclerView时遇到问题。

我不断收到错误:

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>

有人知道问题出在哪里吗?

android android-recyclerview android-bottomsheetdialog
1个回答
1
投票

存在您尚未初始化recycler_provider的问题

recycler_provider = layoutInflater.findViewById<RecyclerView>(R.id.your_recyclerview_id)
© www.soinside.com 2019 - 2024. All rights reserved.