簡體   English   中英

E/RecyclerView:沒有連接適配器; 跳過布局錯誤(FragmentHome)

[英]E/RecyclerView: No adapter attached; skipping layout ERROR (FragmentHome)

我在Fragment中遇到RecyclerView的問題。 FeedActivity中。 我想在我的第一個選項卡中顯示RecyclerView但我不斷收到以下錯誤:

在主頁片段中顯示 recylerview 時出現此錯誤。

recyclerview 根本不顯示在屏幕上。

我試圖在 Oncreate() 下運行它,但沒有成功,我該怎么做?

你能幫我解決這個問題嗎? 在此處輸入圖像描述

**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)
    }


}

在設置recyclerView之后,您應該在 function 的 ed 處返回view

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()

}

一定是這樣的:

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


}

解決方案是這樣的:

解決問題,我把val視圖進行到底,然后又遇到了新的錯誤,邊解決邊理解。

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

重寫樂趣 onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { // 膨脹這個片段的布局

    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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM