Kotlin E/RecyclerView:未连接适配器;跳过布局

发布于 2025-01-16 13:52:30 字数 2601 浏览 1 评论 0原文

大家好,我的新闻应用程序有问题。我的应用程序运行良好。我可以从火力基地获取数据,并且可以在使用调试时将它们写下来。但是当我在虚拟设备回收器视图中打开应用程序时,片段中没有显示并给出此错误:E/RecyclerView:没有连接适配器;跳过布局

我的适配器代码是这样的: `class FeedAdapter(var userList :ArrayList) : RecyclerView.Adapter() {

class MyViewHolder(itemView : View): RecyclerView.ViewHolder(itemView) {
    val comment : TextView = itemView.findViewById(R.id.comment)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {

    val itemView = LayoutInflater.from(parent.context).inflate(R.layout.user_item,parent,false)

    return MyViewHolder(itemView)
}

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
    holder.comment.text = userList[position].comment
}

override fun getItemCount(): Int {
   return userList.size
}

}`

我的 DashboardFragment 是这样的 :

`class DashboardFragment : Fragment() {

private var postArrayList: ArrayList<Post> = arrayListOf()

private var db: FirebaseFirestore ? = null

private var recyclerView: RecyclerView? = null

private var feedAdapter: FeedAdapter? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    db = FirebaseFirestore.getInstance()

    feedAdapter = FeedAdapter(arrayListOf())

    recyclerView?.layoutManager =
        LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)

    recyclerView?.adapter = feedAdapter

    eventChangeListener()


}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return inflater.inflate(R.layout.fragment_dashboard, container, false)
}

@SuppressLint("NotifyDataSetChanged")
private fun eventChangeListener() {

    db?.collection("Posts")?.addSnapshotListener { value, _ ->

        if (value != null) {
            if (!value.isEmpty) {
                val documents = value.documents
                for (document in documents) {
                    val comment: String = (document.get("comment") ?: "").toString()

                    val post = Post(comment)

                    Log.d("avs", post.toString())
                    postArrayList.add(post)
                }

                Log.d("avsArray", postArrayList.toString())

                feedAdapter?.userList = postArrayList

                feedAdapter?.notifyDataSetChanged()
            }
        }


    }


}

}`

找不到解决方案

我 尝试调试我的应用程序,我的 DashboardFragment 不会有任何问题,我可以清楚地写入数据。但是当我尝试调试我的适配器类时,我无法从中得到任何东西

Hello everyone i have a problem with my news app. My app is perfectly working fine. I can get data from fire base and i can write them down while i am using debug. But when i am openin app in virtual device recycler view isn't showing in the fragment and giving this error : E/RecyclerView: No adapter attached; skipping layout

My adapter code is this :
`class FeedAdapter(var userList :ArrayList<Post>) : RecyclerView.Adapter<FeedAdapter.MyViewHolder>() {

class MyViewHolder(itemView : View): RecyclerView.ViewHolder(itemView) {
    val comment : TextView = itemView.findViewById(R.id.comment)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {

    val itemView = LayoutInflater.from(parent.context).inflate(R.layout.user_item,parent,false)

    return MyViewHolder(itemView)
}

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
    holder.comment.text = userList[position].comment
}

override fun getItemCount(): Int {
   return userList.size
}

}`

My DashboardFragment is like this :

`class DashboardFragment : Fragment() {

private var postArrayList: ArrayList<Post> = arrayListOf()

private var db: FirebaseFirestore ? = null

private var recyclerView: RecyclerView? = null

private var feedAdapter: FeedAdapter? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    db = FirebaseFirestore.getInstance()

    feedAdapter = FeedAdapter(arrayListOf())

    recyclerView?.layoutManager =
        LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)

    recyclerView?.adapter = feedAdapter

    eventChangeListener()


}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    return inflater.inflate(R.layout.fragment_dashboard, container, false)
}

@SuppressLint("NotifyDataSetChanged")
private fun eventChangeListener() {

    db?.collection("Posts")?.addSnapshotListener { value, _ ->

        if (value != null) {
            if (!value.isEmpty) {
                val documents = value.documents
                for (document in documents) {
                    val comment: String = (document.get("comment") ?: "").toString()

                    val post = Post(comment)

                    Log.d("avs", post.toString())
                    postArrayList.add(post)
                }

                Log.d("avsArray", postArrayList.toString())

                feedAdapter?.userList = postArrayList

                feedAdapter?.notifyDataSetChanged()
            }
        }


    }


}

}`

i can't find the solution

I have tried to debug my app i wont have any problem with my DashboardFragment i can write data clearly. But when i am trying to debug my adapter class i can't get any thing from that

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人问我粥可暖 2025-01-23 13:52:30
  1. 没有设置recyclerView var(使用findViewById(R.id.yourOwnRecycler)或ViewBinding)
  2. 所有访问视图的代码都必须位于onViewCreated
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    recyclerView = view.findViewById(R.id.yourOwnRecycler)

    recyclerView?.layoutManager =
        LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)

    recyclerView?.adapter = feedAdapter

    eventChangeListener()
}
  1. there is no setting of recyclerView var (use findViewById(R.id.yourOwnRecycler) or ViewBinding)
  2. all code accessing views have to be inside onViewCreated
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    recyclerView = view.findViewById(R.id.yourOwnRecycler)

    recyclerView?.layoutManager =
        LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)

    recyclerView?.adapter = feedAdapter

    eventChangeListener()
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文