onActivityForResult 没有被调用

onActivityForResult doesn't get called

在此代码中,从未调用 onActivityForResult

我的Activity:

class PersonalInformationActivity : AppCompatActivity(), View.OnClickListener {

    private val READ_REQUEST_CODE = 2

    lateinit var user: User
    lateinit var email: EditText
    lateinit var phoneNumber: EditText
    lateinit var username: EditText
    lateinit var profilePicture: ImageView
    lateinit var photoURI: Uri

 //someCode

    override fun onClick(v: View) {
        when (v.id) {
            R.id.profilePicture -> {
                importPicture()
                if (photoURI != null) {
                    try {
                        val file = FileUtil.from(this, photoURI)
                        RequestAddUserProfilePicture.MakeRequestTask(this, this).execute(user.token, file, photoURI.path!!.substring(photoURI.path!!.lastIndexOf("/") + 1))
                        Log.d("file", "File...:::: uti - " + file.path + " file -" + file + " : " + file.exists())

                    } catch (e: IOException) {
                        e.printStackTrace()
                    }

                } else {
                    Toast.makeText(this, "need pic", Toast.LENGTH_LONG).show()
                }
            }
        }
    }

    private fun importPicture() {

        val intent = Intent(Intent.ACTION_PICK,
                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI)

        setResult(Activity.RESULT_OK)
        startActivityForResult(intent, READ_REQUEST_CODE)
    }

    public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            if (data != null) {
                photoURI = data.data
                Log.d("URI", "Uri:" + photoURI.toString())
            }
        }
    }
}

我读过这个答案: 并尝试了技巧但没有解决我的问题。

对 activity 和片段使用不同的 REQUEST_CODES。我尝试了你的代码并尝试了不同的请求代码 101 和 102,我得到了结果。

这不是解决问题的真正方法,但我将其从 Kotlin 重构为 Java(完全相同的代码)并且它也能正常工作...