Kotlin E/RecyclerView:未连接适配器;跳过布局
大家好,我的新闻应用程序有问题。我的应用程序运行良好。我可以从火力基地获取数据,并且可以在使用调试时将它们写下来。但是当我在虚拟设备回收器视图中打开应用程序时,片段中没有显示并给出此错误:E/RecyclerView:没有连接适配器;跳过布局
我的适配器代码是这样的: `class FeedAdapter(var userList :ArrayList
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
recyclerView var
(使用findViewById(R.id.yourOwnRecycler)
或ViewBinding)onViewCreated
内recyclerView var
(usefindViewById(R.id.yourOwnRecycler)
or ViewBinding)onViewCreated