当前位置: 首页 > 知识库问答 >
问题:

回收站查看Android系统错误: E/回收站查看: 没有连接适配器;跳过布局

岳彬炳
2023-03-14

当试图从API获取JSON数据并在RecyclerView上显示时,我得到以下错误:

我已经使用RecyclerView有时,我从来没有这个问题。

主要活动:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(activity_main)
    overwriteOnPostInteractionListener()
    setupObservers()
}

private fun setupObservers(){
    mServiceRequest.searchPostsFromAPI().observe(this, Observer { posts ->
        if (posts != null){
            loadRecyclerView()
            mPostList = posts.toMutableList()
        }
    })
}

private fun loadRecyclerView() {
    recyclerView.adapter = PostListAdapter(mPostList, mOnPostListInteractionListener)
    recyclerView.layoutManager = LinearLayoutManager(this)
    recyclerView.setHasFixedSize(true)
}

适配器:

class PostListAdapter(private val postList: List<PostModel>,
      private val onPostListInteractionListener: OnPostListInteractionListener):
      RecyclerView.Adapter<PostViewHolder>(){ 

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder {
    val inflate = LayoutInflater.from(parent.context)
    val view = inflate.inflate(R.layout.posts , parent, false)
    return PostViewHolder(view, parent.context, onPostListInteractionListener)
}

override fun getItemCount(): Int {
    return postList.count()
}

override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
    holder.bindTask(postList[position])
}}

取景框:

class PostViewHolder(itemView: View, private val context: Context,
                 private val onPostListInteractionListener: OnPostListInteractionListener)
                : RecyclerView.ViewHolder(itemView) {

private val postTitle = itemView.findViewById<TextView>(R.id.titleTextViewMain)
private val postBody = itemView.findViewById<EditText>(R.id.bodyEditText)

fun bindTask(post: PostModel){
    postTitle.text = post.title
    postBody.setText(post.body)
    postTitle.setOnClickListener {
        onPostListInteractionListener.onListClick(post.id)
    }
}}

我搜索了很多如何解决这个错误,但我不能。

共有1个答案

莘钧
2023-03-14

在将数据馈送到列表之前构建RecyclerView适配器,因此需要在实例化适配器之前调用mPostList=posts.toMutableList()

因此,将setupObserver()更改为:

private fun setupObservers(){
    mServiceRequest.searchPostsFromAPI().observe(this, Observer { posts ->
        if (posts != null){
            mPostList = posts.toMutableList()
            loadRecyclerView()

        }
    })
}

在完全构建RecyckerView之后还要设置您的适配器。因此更改loadRecyclerView()的顺序,如下所示。

private fun loadRecyclerView() {
    recyclerView.layoutManager = LinearLayoutManager(this)
    recyclerView.setHasFixedSize(true)
    recyclerView.adapter = PostListAdapter(mPostList, mOnPostListInteractionListener)
}

在布局之前设置< code > recycle view 适配器可能会导致问题。

旁注:

每次列表更改时都要实例化适配器,这不是正确的方法,最好在一个生命周期中只创建一次内容(包括适配器),然后每次希望更改时都创建setter,而不是反复实例化它们。

因此,您只能在< code>onCreate()方法中实例化您的适配器一次,并在您的适配器中创建一个接受posts < code > set posts(private val postList:List

 类似资料:
  • 我有“E/RecyclerView:没有连接适配器;跳过布局”错误消息。我在片段中有recycleView,所以我一定是在fragment1.java上做错了什么。 这是代码。如您所见,我在代码上设置了适配器,但我收到此错误。任何帮助将不胜感激。谢谢。 公共类Fragment1扩展片段{ } 公共类圣经适配器扩展回收器View.Adapter { }

  • 我有一个recycle view,用户可以在其中将一个项目拖放到recycle view中的不同位置。 到目前为止,一切正常。 我的问题始于RecyclerView中的项目超过了它可以显示的数量。所以当它回收他的内容时。当用户将项目拖放到不同的位置,然后滚动离开该零件时,不会保存位置更改。用户只看到项目的旧位置。 您可以在下面的.gif中看到此问题。 我已经尝试了几件事,比如: 和 在我的onBi

  • 我有一些关于适配器的问题,这是我的方法: 另一个Java:1。名单。Java }

  • 图像不显示。在一个SO例子中说要检查适配器中的计数,但我认为它是正确的。我在logcat中得到这个错误 这是代码主活动 这是我的适配器。

  • 我正在为Android编写一些应用程序,在构建项目的过程中,我出现了一个错误:E/RecyclerView:没有连接适配器;跳过布局。应用程序可以工作,但当我想启动这个特定片段(映射片段)时,它会崩溃或再次转到主活动。然而,问题只在于Android 9.0 Pie,而Android 8.0 Oreo没有问题 我尝试了很多东西,但我找不到根本原因,因为在我想要启动的片段上没有RecycleView,

  • 我收到以下错误:“RecyclerView:未连接适配器;跳过布局”,但我已连接适配器...我尝试了许多不同的方法,但我无法解决错误。 我正在使用Volley库获取数据。当我启动活动时,只有进度条可见,直到结束,我收到上面的Logcat消息。我在中添加了带有适配器的。你能帮我吗? 这是我的代码: 主活动 地震适配器 MainActivity.xml 地震法。xml