I have an activity that let user select file from explorer, retrieve the result in onActivityResult()
and save the result inside an object called Property
I have a lateinit variable as follow :
lateinit var uploadProperties: Property
And the code to open explorer (permission already granted) :
fun openExplorer(property: Property) {
uploadProperties = property
val intent = Intent(Intent.ACTION_GET_CONTENT)
intent.type = Constants.ALL_FILE
intent.addCategory(Intent.CATEGORY_OPENABLE)
startActivityForResult(
Intent.createChooser(intent, getString(R.string.select_file)),
REQ_FILE
)
}
an then onActivityResult(), I convert the data to base64 and assign it to the Property
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (resultCode == Activity.RESULT_OK) {
when (requestCode) {
REQ_FILE -> {
data?.let {
val base64 = data.toBase64()
uploadProperties.let {
value = base64
}
}
}
}
}
}
The problem is that at some cases, I got these error report on crashlytics :
Caused by kotlin.UninitializedPropertyAccessException
lateinit property uploadProperties has not been initialized
I tried this many times, and I got these error only a few times (doesn't know what trigger this). But some user complaints that the app always crashed after choosing files from explorer. I checked on crashlytics and the message are as mentioned above.
I have tried to debug using breakpoint before startActivityForResult()
. The variable uploadProperties
is already initialized and the value is correct. But after choosing file from explorer, in some cases, the app still crashed with UninitializedPropertyAccessException
.
Any idea what caused this error and how to fix this?