[英]E/RecyclerView: No adapter attached; skipping layout ERROR (FragmentHome)
I have a problem with RecyclerView
in a Fragment
.我在
Fragment
中遇到RecyclerView
的问题。 In FeedActivity
.在
FeedActivity
中。 I want to show the RecyclerView
in my first tab but I keep getting the following error:我想在我的第一个选项卡中显示
RecyclerView
但我不断收到以下错误:
I am getting this error when displaying recylerview inside home fragment.在主页片段中显示 recylerview 时出现此错误。
recyclerview is not displayed on the screen at all. recyclerview 根本不显示在屏幕上。
I tried to run it under Oncreate() but it didn't work, how can I do it?我试图在 Oncreate() 下运行它,但没有成功,我该怎么做?
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)
}
}
Here you should return the view
at the ed of the function, after you set your recyclerView
:在设置
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()
}
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
}
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.解决问题,我把val视图进行到底,然后又遇到了新的错误,边解决边理解。
binding = FragmentHomeBinding.inflate(layoutInflater) val view = binding.root
绑定 = FragmentHomeBinding.inflate(layoutInflater) val view = binding.root
override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View?重写乐趣 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.