首页 > 解决方案 > E/RecyclerView:没有附加适配器;在 kotlin 中跳过布局

问题描述

** 我想知道我犯了什么错误 跳过布局,我还不能解决请告诉我如何解决这个错误.................................. ..................................................... ..................................................... ......**

搜索.xml

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recycler_view_search"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/app_bar_layout_search"
        android:visibility="gone">

    </androidx.recyclerview.widget.RecyclerView>

搜索.kt

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val view=inflater.inflate(R.layout.fragment_search, container, false)

        recyclerView=view.findViewById(R.id.recycler_view_search)
        recyclerView?.setHasFixedSize(true)
        recyclerView?.layoutManager=LinearLayoutManager(context)
        mUser=ArrayList()
        userAdapter=context?.let{ UserAdapter(it,mUser as 
       ArrayList<User>,true) }
        recyclerView?.adapter=userAdapter
        view.search_edit_text.addTextChangedListener(object: TextWatcher{


            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
                TODO("Not yet implemented")
            }


       

标签: kotlinandroid-recyclerview

解决方案


<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/rv_hashtags"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
    android:orientation="horizontal"
    tools:listitem="@layout/item_trending"/
  1. 检查您正在使用的上下文。如果它为空,则适配器可能无法连接,并且可能会发生上述错误
  2. app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" 这行可以帮你摆脱上面的错误。

推荐阅读