【发布时间】: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