lateinit属性数据尚未初始化 [英] lateinit property data has not been initialized
问题描述
未使用Retrofit2和rxjava2在Recyclerview中设置Gson Convertable数据,然后通过其Give Give错误进行预订:
Gson Convertable data is not set in Recyclerview using Retrofit2 and rxjava2 then subscribe throughble its Give Error:
UninitializedPropertyAccessException: lateinit property data has not been initialized
通过Retrofit2和rxjava2进行JSON数据解析.解析GSON数据转换GSon时,rxjava2订阅数据,然后给出lateinit属性错误,并且未在recyclerview中设置该错误.
JSON data parsing by retrofit2 and rxjava2. when parsing GSON Data Converting GSon, rxjava2 Subscribe the data then give lateinit property error and its not set in recyclerview.
MainActivity.kt
MainActivity.kt
class Company : AppCompatActivity() {
internal lateinit var api : APIInterface
var compositeDisposable = CompositeDisposable()
internal lateinit var companyDialog : Dialog
internal lateinit var adapter: CompanyAdapter
internal lateinit var data : List<Company>
internal lateinit var rvCompany : RecyclerView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_company)
companyDialog = Dialog(this)
//companyAdapter = CompanyAdapter()
btnSelectCompany.setOnClickListener{
showCompanyPopupView()
}
}
fun showCompanyPopupView(){
companyDialog.setContentView(R.layout.compny_popup_screen)
rvCompany = companyDialog.findViewById(R.id.rvCompany)
rvCompany.setHasFixedSize(true)
rvCompany.layoutManager = LinearLayoutManager(this) as RecyclerView.LayoutManager?
fetchData()
companyDialog.window.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
companyDialog.show()
}
private fun fetchData(){
val retrofit = APIClient.apIClient
if (retrofit != null) {
api = retrofit.create(APIInterface::class.java)
}
compositeDisposable.add(api.getCompanyData()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe ({ companyList-> displayData(data)
},{
Toast.makeText(applicationContext, it.message, Toast.LENGTH_SHORT).show()
})
)
}
private fun displayData(companyList: List<Company>) {
adapter = CompanyAdapter(this,companyList)
rvCompany.adapter = adapter
}
}
CompanyAdapter.kt
CompanyAdapter.kt
class CompanyAdapter(internal var context: Context, internal var companyList: List<Company>)
:RecyclerView.Adapter<CompanyAdapter.CompanyViewHolder>()
{
override fun onCreateViewHolder(p0: ViewGroup, p1: Int): CompanyViewHolder {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
val itemView = LayoutInflater.from(p0.context).inflate(R.layout.list_view_item,p0,false)
return CompanyViewHolder(itemView)
}
override fun getItemCount(): Int {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
return companyList?.size!!
}
override fun onBindViewHolder(p0: CompanyViewHolder, p1: Int) {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
// p0.rbButton.text = this!!.companyList?.get(p1)?.Cmp_Name
p0.bindModel(companyList[p1])
}
inner class CompanyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
val radioButton : RadioButton = itemView.findViewById(R.id.rbCompanyName)
fun bindModel(company: Company){
radioButton.text = company.Cmp_Name
}
}
}
推荐答案
发生此错误是因为您从未对其进行初始化(data = ...
),而是在{ companyList-> displayData(data) }
中对其进行了访问.这会忽略您从subscribe
获得的companyList
,这可能并不是您真正想要的.
The error happens because you never initialize it (data = ...
), but you access it in { companyList-> displayData(data) }
. This ignores the companyList
you get from subscribe
, which is probably not what you actually want.
您的代码似乎过度使用了lateinit
.当您真的需要它时使用它.
Your code seems to overuse lateinit
a lot. Use it when you really need it.
这篇关于lateinit属性数据尚未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!