gpt4 book ai didi

kotlin - Lateinit 属性数据尚未初始化

转载 作者:行者123 更新时间:2023-12-02 02:17:43 26 4
gpt4 key购买 nike

使用 Retrofit2 和 rxjava2 未在 Recyclerview 中设置 Gson 可转换数据,然后通过其订阅给出错误:

UninitializedPropertyAccessException: lateinit property data has not been initialized

通过retrofit2和rxjava2解析JSON数据。解析GSON数据转换GSon时,rxjava2订阅数据然后给出lateinit属性错误并且在recyclerview中未设置。

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

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,这可能不是您真正想要的。

您的代码似乎经常过度使用lateinit。当您真正需要它时使用它。

关于kotlin - Lateinit 属性数据尚未初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55429004/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com