Recyclerview 出现问题。 recyclerview.xml 不能为空

Trouble with Recyclerview. recyclerview.xml must no be null

我有 xml 和 recyclerview,id = list_chat

<androidx.constraintlayout.widget.ConstraintLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context=".ui.chats.ChatsFragment">

   <androidx.recyclerview.widget.RecyclerView
       android:id="@+id/list_chat"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
   </androidx.recyclerview.widget.RecyclerView>

</androidx.constraintlayout.widget.ConstraintLayout> 

我的 Fragment 将是带有函数 uploadList 的 recyclerview,它将数据放入我的 recyclerView 数组中

class ChatsFragment : Fragment() {

    var list: MutableList<Chat> = ArrayList()
    lateinit var adapter: ChatListAdapter
    lateinit var manager: RecyclerView.LayoutManager


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        uploadList()
        Log.e("TAG", list.toString())

    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view: View = inflater.inflate(R.layout.fragment_chats, container, false)
        uploadList()
        manager = LinearLayoutManager(activity)
        list_chat.layoutManager = manager
        adapter = ChatListAdapter(list)
        list_chat.adapter = adapter
        return view
    }
    
    private fun uploadList(){
        list.add(Chat("11111", "11111"))
        list.add(Chat("11222221", "133311"))
        list.add(Chat("1122221", "114444411"))
        list.add(Chat("112222211", "5555555"))
    }
}

xml 包含列表项。 ImageView 暂时无所谓。只需要两个textView

<?xml version="1.0" encoding="utf-8"?>
<layout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

<LinearLayout
    android:id="@+id/item_list"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/chatImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@mipmap/ic_launcher"/>

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginStart="5sp">
        <TextView
            android:id="@+id/nameChat"
            android:text="2222222222222"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
        <TextView
            android:id="@+id/infoChat"
            android:text="11111111"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>
</LinearLayout>
</layout>

和我的自定义适配器

class ChatListAdapter(private val myDataset: MutableList<Chat>) :RecyclerView.Adapter<ChatListAdapter.ViewHolder>() {



    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_list, parent, false)
        return ViewHolder(layoutInflater)
    }


    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(myDataset[position])

    }


    override fun getItemCount() = myDataset.size

    class ViewHolder(v: View) : RecyclerView.ViewHolder(v){
        private val nameChat: TextView = itemView.findViewById(R.id.nameChat)
        private val infoChat: TextView = itemView.findViewById(R.id.infoChat)

        fun bind(chat: Chat){
            nameChat.text = chat.name
            infoChat.text = chat.info
        }

    }
}

我在这里抓到 java.lang.IllegalStateException: list_chat must not be null list_chat.layoutManager = manager

帮帮我,我只有list_chat在fragment_chats,所以是recyclerview。 因为请帮帮我,我不知道是什么问题

在 onViewCreated() 方法中初始化布局管理器和适配器

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
         uploadList()
        manager = LinearLayoutManager(activity)
        list_chat.layoutManager = manager
        adapter = ChatListAdapter(list)
        list_chat.adapter = adapter
        })

您是否使用 kotlinx.android.synthetic 来查找您的观点?如果是这样,这些在 onCreateView() returns 之后才有效,因此您不能在 onCreateView().

中使用它们

将您的代码移至 onViewCreated(),或切换到从 onCreateView() 内部手动调用 view.findViewById()

方法一:

创建 recyclerview object :

val rv = view.findViewById(your rv id)

并为其分配布局管理器和适配器。

方法二:

在您的布局文件中,在 recyclerview 中添加此行:

<androidx.recyclerview.widget.RecyclerView
        <!-- your default lines -->
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
 />

并在 onViewCreated method 中分配适配器。