lateinit属性数据尚未初始化

时间:2019-03-30 06:58:42

标签: kotlin android-recyclerview retrofit2 rx-java2

未使用Retrofit2和rxjava2在Recyclerview中设置Gson Convertable数据,然后通过其Give Give错误进行预订:

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
        }
    }



}

2 个答案:

答案 0 :(得分:1)

发生错误是因为您从未对其进行初始化(data = ...),而是在{ companyList-> displayData(data) }中对其进行了访问。这会忽略您从companyList获得的subscribe,这可能并不是您真正想要的。

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

答案 1 :(得分:0)

您正在data函数中使用未初始化的变量displayData(data)。而是通过companyList列出您从subscribe函数中得到的结果,如下所示。

 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(companyList)
                },{
                    Toast.makeText(applicationContext, it.message, Toast.LENGTH_SHORT).show()
                })

        )

    }

另外,您可以先保存companyList变量中的data,然后再使用它来避免错误。这样做将帮助您在类companyList的其他位置使用此Company,而不仅仅是传递函数。

compositeDisposable.add(api.getCompanyData()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe ({ companyList-> 
                        data = companyList
                        displayData(data)
                },{
                    Toast.makeText(applicationContext, it.message, Toast.LENGTH_SHORT).show()
                })

        )