簡體   English   中英

仍然沒有解決錯誤 E/RecyclerView:沒有連接適配器; 跳過布局

[英]Still Not Gettiing The Error Solved E/RecyclerView: No adapter attached; skipping layout

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

我在 inte.net 上搜索過,但我得到的所有解決方案都沒有幫助我。

request.getComplaints是一個異步回調,可能會在布局膨脹后延遲。

您可以在OnCreate方法或其他適當的地方設置適配器,然后在上面的回調中使用notifyDataSetChanged()更新適配器。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM