未使用Retrofit2和rxjava2在Recyclerview中设置Gson可转换数据,然后给出其给定错误:
“ kotlin.UninitializedPropertyAccessException:lateinit属性CompositeDisposable尚未初始化”
MainActivity.kt
internal lateinit var api : APIInterface
lateinit var compositeDisposable: CompositeDisposable
internal lateinit var companyDialog : Dialog
internal lateinit var companyAdapter: CompanyAdapter
internal lateinit var data : List<Company>
fun showCompanyPopupView(){
companyDialog.setContentView(R.layout.compny_popup_screen)
val rvCompany : RecyclerView = companyDialog.findViewById(R.id.rvCompany)
rvCompany.setHasFixedSize(true)
rvCompany.layoutManager = LinearLayoutManager(this)
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>) {
val adapter = CompanyAdapter(this,companyList)
rvCompany.adapter = adapter
}
CompanyAdapter.kt
class CompanyAdapter(内部var上下文:上下文,内部var companyList:列表) :RecyclerView.Adapter(){ 重写onCreateViewHolder(p0:ViewGroup,p1:Int)的乐趣:CompanyViewHolder { TODO(“未实现”)//要更改已创建函数的主体,请使用File |设置|文件模板。
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
}
}
}
答案 0 :(得分:1)
错误已得到很好的描述,您在初始化变量之前正在使用您的变量CompositeDisposable。
只需在调用变量之前以这种方式初始化变量即可:
internal lateinit var api : APIInterface
var compositeDisposable = CompositeDisposable()
internal lateinit var companyDialog : Dialog
internal lateinit var companyAdapter: CompanyAdapter
internal lateinit var data : List<Company>
fun showCompanyPopupView(){
companyDialog.setContentView(R.layout.compny_popup_screen)
val rvCompany : RecyclerView = companyDialog.findViewById(R.id.rvCompany)
rvCompany.setHasFixedSize(true)
rvCompany.layoutManager = LinearLayoutManager(this)
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()
})
)
}