【问题标题】:Still Not Gettiing The Error Solved E/RecyclerView: No adapter attached; skipping layout仍然没有解决错误 E/RecyclerView:没有连接适配器;跳过布局
【发布时间】:2020-08-18 07:17:07
【问题描述】:

我是 Kotlin 的新手,我一直在开发这个应用程序,在连接 API 之前,我能够查看应用的示例数据。连接后出现错误

E/RecyclerView:没有附加适配器;跳过布局

下面是我的片段

package com.example.testapp.ui.complaints

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import com.example.testapp.R
import com.example.testapp.ui.ApiData.ApiDataEndPoints
import com.example.testapp.ui.ApiData.Complaints
import com.example.testapp.ui.ApiData.ServiceBuilder
import kotlinx.android.synthetic.main.fragment_complaints.*
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class ComplaintsFragment: Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?): View? {
        //inflating the layout for this fragment
        //the fragment class calls our fragment layout
        return inflater.inflate(R.layout.fragment_complaints, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        //creating a call of the ApiEndPoints interface
        val request = ServiceBuilder.buildService(ApiDataEndPoints::class.java)

        val call = request.getComplaints(getString(R.string.api_key))

        call.enqueue(object : Callback<Complaints> {
            override fun onResponse(call: Call<Complaints>, response: Response<Complaints>){
                if(response.isSuccessful){
                    progress_bar.visibility = View.GONE

                    text_complaints.apply{
                        setHasFixedSize(true)
                        // set the LinearLayoutManager to handle the Android
                        //Recyclerview behavior
                        layoutManager = LinearLayoutManager(requireActivity()) //removed activity and put requireActivity()
                        // set the custom adapter to the RecyclerView
                        adapter = ComplaintsAdapter(response.body()!!.results)
                    }
                }
            }

            override fun onFailure(call: Call<Complaints>,t: Throwable){
                Toast.makeText(requireActivity(), "${t.message}", Toast.LENGTH_SHORT).show()
            }
        })
    }
}

我在互联网上进行了搜索,但我得到的所有解决方案都对我没有帮助。

【问题讨论】:

  • @Abhimanyu 它是,它在应用块内

标签: android kotlin android-recyclerview


【解决方案1】:

request.getComplaints是异步回调,布局膨胀后可能会延迟。

您可以在OnCreate 方法或其他适当的地方设置适配器,然后在上述回调中使用notifyDataSetChanged() 更新适配器。

【讨论】:

  • 请问我该怎么做?
猜你喜欢
  • 2023-02-06
  • 2019-04-13
  • 1970-01-01
  • 2016-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多