1

我有一个活动让用户从资源管理器中选择文件,检索结果onActivityResult()并将结果保存在一个名为Property

我有一个 lateinit 变量如下:

lateinit var uploadProperties: Property

以及打开资源管理器的代码(已授予权限):

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

然后 onActivityResult(),我将数据转换为 base64 并将其分配给属性

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

问题是,在某些情况下,我收到了有关 crashlytics 的这些错误报告:

Caused by kotlin.UninitializedPropertyAccessException
lateinit property uploadProperties has not been initialized

我尝试了很多次,但只有几次出现这些错误(不知道是什么触发了这个)。但一些用户抱怨应用程序在从资源管理器中选择文件后总是崩溃。我检查了 crashlytics,消息如上所述。

我之前尝试过使用断点进行调试startActivityForResult()。变量uploadProperties已经初始化并且值是正确的。但是在从资源管理器中选择文件后,在某些情况下,应用程序仍然会以UninitializedPropertyAccessException.

知道是什么导致了这个错误以及如何解决这个问题吗?

4

1 回答 1

1

您需要处理应用程序进程被破坏以释放内存的情况。在您的情况下,存储uploadProperties应该足够了。

在您的 Activity 中,首先在它被销毁时存储它(我假设您的Property类是Parcelable,如果不写任何您需要稍后恢复它的东西):

override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)
    // ( ... your save instance states if any)
    // store upload properties if they were set up
    if(::uploadProperties.isInitialized)
        outState.putParcelable("uploadProperties", uploadProperties)
}

然后在你的某处恢复它onCreate

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    // (...)
    if(savedInstanceState?.containsKey("uploadProperties") == true){
        uploadProperties = savedInstanceState.getParcelable("uploadProperties")!!
    }
    // (...)
}

现在更改您的结果回调,使其延迟到您的属性恢复后:

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()
                    // delay using androidx.lifecycle
                    lifecycleScope.launchWhenCreated {
                        uploadProperties.let { it.value = base64 }
                    }
                }
            }
        }
    }
}
于 2020-02-29T16:42:37.880 回答