0

我正在使用以下功能来请求许可。问题是如果我在 onMapReady 中使用它,它会给出此错误消息"MapsActivity@cab019a is attempting to register while current state is RESUMED. LifecycleOwners must call register before they are STARTED.",如果我这次在 onCreate() 中使用它,我该it's giving mMap lateinit property has not been initialized如何解决。我需要在哪里使用 mMap.isMyLocationEnabled

private fun requestPermission(){

    val requestPermissionLauncher =
        registerForActivityResult(
            ActivityResultContracts.RequestPermission()
        ) { isGranted: Boolean ->
            if (isGranted) {
                Toast.makeText(this,"PERMISSION GRANTED", Toast.LENGTH_LONG).show()
                mMap.isMyLocationEnabled=true


            } else {
                Toast.makeText(this,"SHUTTING DOWN", Toast.LENGTH_LONG).show()

            }
        }

    when (PackageManager.PERMISSION_GRANTED) {
        ContextCompat.checkSelfPermission(
            this,
            Manifest.permission.ACCESS_FINE_LOCATION
        ) -> {
            Toast.makeText(this,"PERMISSION'S ALREADY GRANTED", Toast.LENGTH_LONG).show()
            mMap.isMyLocationEnabled=true


        }
        else -> {
            requestPermissionLauncher.launch(
                Manifest.permission.ACCESS_FINE_LOCATION)
        }
    }
}
4

0 回答 0