本文介绍了如何解决错误"java.lang.IllegalStateException:RecyclerView不能为空"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个recyclerView,我在其中使用适配器发送数据,但收到错误

I have a recyclerView where I send data with my adapter but got the error

使用我的adpater编码:

code with my adpater:

class InWaitingFragment : Fragment() {

    private lateinit var adapter: FastItemAdapter<BarterWaitingItem>

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?

    ): View? {

        println("CONTAINER:  " + container)

        val inflate: FrameLayout = inflater.inflate(R.layout.fragment_in_waiting, container, false) as FrameLayout

        adapter = FastItemAdapter<BarterWaitingItem>()

       waitingRecyclerView.layoutManager = LinearLayoutManager(this)

       waitingRecyclerView.adapter = adapter

        val retrofit = Retrofit.Builder()
            .baseUrl("http://10.93.182.95:8888")
            .addConverterFactory(GsonConverterFactory.create())
            .build()

        val service = retrofit.create(RequestManager::class.java)

        val action  = service.getPending()


        action.enqueue(object: Callback<ArrayList<GetBarterResponse>> {
            override fun onResponse(
                call: Call<ArrayList<GetBarterResponse>>,
                response: Response<ArrayList<GetBarterResponse>>
            ) {
                val allBarter = response.body()
                if(allBarter != null){
                    for (c in allBarter){
                        println("OBJET: ${c.buyer_object.title}")
                    }

                    println("ONRESPONSE En attente: " + response.body().toString())
                }
            }

            override fun onFailure(call: Call<ArrayList<GetBarterResponse>>, t: Throwable) {
                println("ONFAILURE En attente: " + t.toString())
            }
        })

        return inflate
    }


}

LinearLayoutManager(this)的this上出错了,说:

got an error to on the this of LinearLayoutManager(this), says:

`require:Context!
 Founds: InWaitingFragment

推荐答案

您应将 LinearLayoutManager(this)更改为 LinearLayoutManager(this.context)

这篇关于如何解决错误"java.lang.IllegalStateException:RecyclerView不能为空"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 17:57