android - Cannot solve this error. Kotlin -Firebase
问题描述
I need to get those data to a recyclerview, so I created an adapter class and items class. There's no issues. but this is the error I got:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.busconductor/com.example.busconductor.busCodesActivity}: kotlin.UninitializedPropertyAccessException: lateinit property reference has not been initialized
I did all right, but cannot solve this.
This's the code
class busCodesActivity : AppCompatActivity() {
private lateinit var reference : FirebaseDatabase1
private lateinit var codeRecyclerView: RecyclerView
private lateinit var codeArrayList: ArrayList<CityCodes>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_bus_codes)
codeRecyclerView = findViewById(R.id.recyclerView)
codeRecyclerView.layoutManager = LinearLayoutManager(this)
codeRecyclerView.setHasFixedSize(true)
codeArrayList = arrayListOf<CityCodes>()
getCodeDetails()
val Button = findViewById<Button>(R.id.button5)
Button.setOnClickListener {
startActivity(Intent(this@busCodesActivity, conductorMenu::class.java))}
}
private fun getCodeDetails() {
val ref = reference.getReference("CityCodes")
ref.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
if (snapshot.exists()){
for (routeSnapshot in snapshot.children){
val code = routeSnapshot.getValue(CityCodes::class.java)
codeArrayList.add(code!!)
}
codeRecyclerView.adapter = CityAdapter(codeArrayList)
}
}
override fun onCancelled(error: DatabaseError) {
TODO("Not yet implemented")
}
})
}
}
Please help me. I'm new to kotlin.
解决方案
As the error message says, you never initialize reference
that you declared here:
private lateinit var reference : FirebaseDatabase1
The 1
in FirebaseDatabase1
is unexpected, but I'm going to assume you know where that comes from. If not, you probably want it to be FirebaseDatabase
without the 1
.
My guess is that you want to initialize that in onCreate
to point to the database:
reference = Firebase.database
推荐阅读
- c# - MVC - 回发后引导 datetimepicker 控件重置
- c# - 当我们在 Linq 上使用 .Select(x=>new object) 时,为什么我们不能在多方面选择任何属性
- javascript - 如何将对象数组转换为位置改变的数组
- git - 从 bitbucket 服务器中删除存储库的数据
- android - 如何使颤振小部件扩展到其父小部件之外
- selenium-webdriver - 如何在 Eclipse 中执行 TestNG 项目的 jar 文件
- c - 如何找到dll的最小和最大地址
- python - 从python中的输出信号中去除周期性噪声信号
- openlayers - 如何在 OpenLayers-6 中为绘制的多边形添加边距?
- visual-studio - xunit 测试在 Visual Studio 2019 .netcore 5 中意外停止工作