android - 代码在 lambda 表达式之外工作,但不能在内部工作

标签 android lambda kotlin return

我有以下代码:

override fun onCreate(savedInstanceState: Bundle?) {
    ...
    fab_action.setOnClickListener(actionSetMyLocationEnable) //passing my lambda
}

private val actionSetMyLocationEnable: (View) -> Unit = { it as FloatingActionButton
    it.isSelected = !it.isSelected
    setMyLocationEnable(it.isSelected) //this call work fine
}

private fun setMyLocationEnable(enable: Boolean) {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
                PERMISSION_REQUEST_ACCESS_FINE_LOCATION)
        return //and this return work nice too
    }
    mMap.isMyLocationEnabled = enable
}

但是,当我按如下方式应用它时:

private val actionSetMyLocationEnable: (View) -> Unit = { it as FloatingActionButton
    it.isSelected = !it.isSelected
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
                PERMISSION_REQUEST_ACCESS_FINE_LOCATION)
        return //error 1
    }
    mMap.isMyLocationEnabled = it.isSelected //error 2
}

我遇到了这两个错误:

错误 1.

'return' is not allowed here

错误2.

Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with checkPermission) or explicitly handle a potential SecurityException

我知道每个错误的含义,但是

我的问题是:为什么我的代码在 lambda 表达式之外工作而在内部却不起作用?我该如何解决这个问题?

已更新

@Rene Ferrari's solution解决错误 1。非常感谢@Rene Ferrari

最佳答案

基于 mTak 回答我的解决方案如下:

private val actionSetMyLocationEnable: (View) -> Unit = returnHere@{ it as FloatingActionButton
    it.isSelected = !it.isSelected
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(
                this,
                arrayOf(Manifest.permission.ACCESS_FINE_LOCATION),
                PERMISSION_REQUEST_ACCESS_FINE_LOCATION)
        return@returnHere //error 1
    }
    mMap.isMyLocationEnabled = it.isSelected //error 2
}

您基本上可以定义要返回的标签。这个标签当然可以命名为关键字以外的任何名称。在此示例中,我将其命名为 returnHere

关于android - 代码在 lambda 表达式之外工作,但不能在内部工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51312442/

相关文章:

android - 为什么 Android Datastore 总是通过运行阻塞返回相同的值

android - Executor 的 Runnable 中的变量

java - 如何使用我的 Android 应用加载 friend 的 Facebook 信息

android - 使用 jack 时,Lambda 表达式因 Android 中的 IncompatibleClassChangeError 而崩溃

c++ - 如何使用 for_each 获取 vector 中值的索引?

android - 是否可以使 LazyRow/LazyColumn 在屏幕上显示一定数量的项目,而无需传入显式 DP 宽度

Android,Room 在使用 Robolectric 进行单元测试时失败

android - JSoup如何选择和获取具体信息

android - 将 Android 应用程序更新到最新版本的 OpenSSL

java - Java-8 中的方法引用和泛型