仍未解决错误的E / RecyclerView:未连接适配器;跳过布局

时间:2020-08-18 07:17:07

标签: android kotlin android-recyclerview

我是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()
            }
        })
    }
}

我已经在互联网上进行搜索,但是我到达那里的所有解决方案都帮不了我。

1 个答案:

答案 0 :(得分:2)

request.getComplaints是一个异步回调,版式膨胀后可能会延迟。

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