RecyclerView报错No adapter attached; skipping layout 求排查解决
RecyclerView「No adapter attached; skipping layout」错误排查求助
我遇到了RecyclerView的「No adapter attached; skipping layout」错误,尝试了网上几乎所有解决方案仍未解决。以下是我的相关代码,希望有人能帮忙找出问题所在:
val recyclerview = view.findViewById<RecyclerView>(R.id.memberRecyclerView) val layoutManager: RecyclerView.LayoutManager = LinearLayoutManager(activity) recyclerview.setLayoutManager(layoutManager) recyclerview.setHasFixedSize(true) val adapter = MemberListAdapter(data) recyclerview.setAdapter(adapter)
AddingGroup.kt
class AddingGroup : Fragment() { //Declaring private lateinit var group_name : EditText private lateinit var member_email : EditText private lateinit var imageAdd1 : ImageView private lateinit var btn_submit : Button override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) } override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { // Inflate the layout for this fragment val view = inflater.inflate(R.layout.fragment_adding_group, container, false) group_name = view.findViewById(R.id.group_name) member_email = view.findViewById(R.id.member_email) imageAdd1 = view.findViewById(R.id.imageAdd1) btn_submit = view.findViewById(R.id.btn_submit) val recyclerview = view.findViewById<RecyclerView>(R.id.memberRecyclerView) val layoutManager: RecyclerView.LayoutManager = LinearLayoutManager(activity) recyclerview.setLayoutManager(layoutManager) recyclerview.setHasFixedSize(true) //adding member email to array list val data = ArrayList<MemberList>() imageAdd1.setOnClickListener{ data.add(MemberList(member_email.toString(), R.drawable.ic_remove_circle)); } val adapter = MemberListAdapter(data) recyclerview.setAdapter(adapter) //adding group to firebase btn_submit.setOnClickListener { // } return view } }
MemberListAdapter.kt
class MemberListAdapter(private val member_list: ArrayList<MemberList>) : RecyclerView.Adapter<ViewHolder>() { override fun getItemCount(): Int { return member_list.size } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { val view = LayoutInflater.from(parent.context) .inflate(R.layout.fragment_group_card, parent, false) return ViewHolder(view) } override fun onBindViewHolder(holder: ViewHolder, position: Int) { val MemberList = member_list[position] // sets the image to the imageview from our itemHolder class holder.imageView.setImageResource(MemberList.removeBtn) // sets the text to the textview from our itemHolder class holder.textView.text = MemberList.memberEmail } } class ViewHolder(ItemView: View) : RecyclerView.ViewHolder(ItemView) { val imageView: ImageView = itemView.findViewById(R.id.removebtn) val textView: TextView = itemView.findViewById(R.id.usermail) }
RecyclerView布局
<androidx.recyclerview.widget.RecyclerView android:id="@+id/memberRecyclerView" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintBottom_toTopOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="parent" app:layout_constraintVertical_bias="0.288" tools:ignore="MissingConstraints" tools:itemCount="5" tools:listitem="@layout/fragment_group_card" app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
fragment_group_card.xml
<?xml version="1.0" encoding="utf-8"?> <FrameLayout 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" xmlns:app="http://schemas.android.com/apk/res-auto"> <androidx.cardview.widget.CardView android:layout_width="match_parent" android:layout_height="match_parent"> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/usermail" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" android:layout_width="match_parent" android:layout_height="50dp" android:hint="UserName" android:textColor="#000000" android:textAppearance="@style/TextAppearance.AppCompat.Large" tools:layout_editor_absoluteX="16dp" tools:layout_editor_absoluteY="25dp" /> <ImageView android:id="@+id/removebtn" android:layout_width="wrap_content" android:layout_height="25dp" app:srcCompat="@drawable/ic_remove_circle" tools:ignore="MissingConstraints" tools:layout_editor_absoluteX="350dp" tools:layout_editor_absoluteY="6dp" /> </androidx.constraintlayout.widget.ConstraintLayout> </androidx.cardview.widget.CardView> </FrameLayout>
感谢各位的帮助!
内容的提问来源于stack exchange,提问作者Jayashree K
相关产品推荐
相关产品推荐

