Home > Mobile >  E/RecyclerView: No adapter attached; skipping layout ERROR (FragmentHome)
E/RecyclerView: No adapter attached; skipping layout ERROR (FragmentHome)

Time:01-15

I have a problem with RecyclerView in a Fragment. In FeedActivity. I want to show the RecyclerView in my first tab but I keep getting the following error:

I am getting this error when displaying recylerview inside home fragment.

recyclerview is not displayed on the screen at all.

I tried to run it under Oncreate() but it didn't work, how can I do it?

Can you help me how can I solve this?enter image description here

**class HomeFragment : Fragment() {**

    private lateinit var auth : FirebaseAuth
    private lateinit var db : FirebaseFirestore
    private lateinit var binding: FragmentHomeBinding

    val postArrayList : ArrayList<Post> = ArrayList()
    var adapter : FeedRecyclerAdapter? = null

    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_home, container, false)
        return view

       ** binding.recyclerView.layoutManager = LinearLayoutManager(activity)

        adapter = FeedRecyclerAdapter(postArrayList)
        binding.recyclerView.adapter = adapter**


        auth = FirebaseAuth.getInstance()
        db = FirebaseFirestore.getInstance()

        getDataFromFirestore()

    }


    fun getDataFromFirestore() {

        db.collection("Posts").orderBy("date",
            Query.Direction.DESCENDING).addSnapshotListener { snapshot, exception ->
            if (exception != null) {
                Toast.makeText(context,exception.localizedMessage, Toast.LENGTH_LONG).show()
            } else {

                if (snapshot != null) {
                    if (!snapshot.isEmpty) {

                        postArrayList.clear()

                        val documents = snapshot.documents
                        for (document in documents) {
                            val comment = document.get("comment") as String
                            val useremail = document.get("userEmail") as String
                            val downloadUrl = document.get("downloadUrl") as String
                            //val timestamp = document.get("date") as Timestamp
                            //val date = timestamp.toDate()

                            val post = Post(useremail,comment, downloadUrl)
                            postArrayList.add(post)
                        }
                        adapter!!.notifyDataSetChanged()

                    }
                }

            }
        }

    }


}
**class FeedRecyclerAdapter(private val postList : ArrayList<Post>) : RecyclerView.Adapter<FeedRecyclerAdapter.PostHolder>() {**

    class PostHolder(val binding: RecyclerRowBinding) : RecyclerView.ViewHolder(binding.root) {

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostHolder {
        val binding = RecyclerRowBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        return PostHolder(binding)
    }

    override fun getItemCount(): Int {
        return postList.size

    }

    override fun onBindViewHolder(holder: PostHolder, position: Int) {
        holder.binding.recyclerEmailText.text = postList.get(position).email
        holder.binding.recyclerCommentText.text = postList.get(position).comment
        Picasso.get().load(postList[position].downloadUrl).into(holder.binding.recyclerImageView)
    }


}

CodePudding user response:

Here you should return the view at the ed of the function, after you set your recyclerView:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {

// Inflate the layout for this fragment

val view = inflater.inflate(R.layout.fragment_home, container, false)

return view //here this should be at the end

** binding.recyclerView.layoutManager = LinearLayoutManager(activity)

adapter = FeedRecyclerAdapter(postArrayList)
binding.recyclerView.adapter = adapter**

auth = FirebaseAuth.getInstance()
db = FirebaseFirestore.getInstance()

getDataFromFirestore()

}

Must be like this:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,savedInstanceState: Bundle?): View? {

// Inflate the layout for this fragment

val view = inflater.inflate(R.layout.fragment_home, container, false)
    
** binding.recyclerView.layoutManager = LinearLayoutManager(activity)

adapter = FeedRecyclerAdapter(postArrayList)
binding.recyclerView.adapter = adapter**

auth = FirebaseAuth.getInstance()
db = FirebaseFirestore.getInstance()

getDataFromFirestore()

return view //should be here


}

CodePudding user response:

The solution is like this :

To solve the problem, I took the val view to the end, and then I encountered a new error, which I understood while solving it.

binding = FragmentHomeBinding.inflate(layoutInflater) val view = binding.root

override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { // Inflate the layout for this fragment

    binding = FragmentHomeBinding.inflate(layoutInflater)
    val view = binding.root


    binding.recyclerView.layoutManager = LinearLayoutManager(activity)

    adapter = FeedRecyclerAdapter(postArrayList)
    binding.recyclerView.adapter = adapter


    auth = FirebaseAuth.getInstance()
    db = FirebaseFirestore.getInstance()

    getDataFromFirestore()

    return view
  • Related