函数调用中的“意外令牌”错误

时间:2019-05-25 09:53:29

标签: android kotlin

我在使用Kotlin语法时遇到麻烦,它需要在函数调用(?!)中使用分号。

我只是试图从AltBeacon网站(用于获取粗略位置permissions)的代码进行转换。这是我急着想出的:

class MainActivity : AppCompatActivity() {



    val TAG = "MAIN_ACTIVITY"
    val PERMISSION_REQUEST_COARSE_LOCATION = 1


    override fun  onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
            if (checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
                val  builder = AlertDialog.Builder(this)
                builder.setTitle("This app needs location access")
                builder.setMessage("Please grant location access so this app can detect beacons.")
                builder.setPositiveButton(android.R.string.ok, null)
                builder.setOnDismissListener {
                    val permissions = arrayOf(Manifest.permission.ACCESS_COARSE_LOCATION)
                    requestPermissions(permissions, PERMISSION_REQUEST_COARSE_LOCATION)

                }
                builder.show()

            }
        }

    }


    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>,  grantResults: IntArray) {
            when (requestCode) {
                PERMISSION_REQUEST_COARSE_LOCATION-> {
                if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    Log.d(TAG, "coarse location permission granted")
                } else {
                    val builder = AlertDialog.Builder(this)
                    builder.setTitle("Functionality limited")
                    builder.setMessage("Since location access has not been granted, this app will not be able to discover beacons when in the background.");
                    builder.setPositiveButton(android.R.string.ok, null)
                    builder.setOnDismissListener { }
                    builder.show()
                }
            }
        }
    }



}

编译器存在以下问题:

requestPermissions(permissions, PERMISSION_REQUEST_COARSE_LOCATION)

Unexpected tokens (use ';' to separate expressions on the same line)

1 个答案:

答案 0 :(得分:3)

使用kotlin语法,一切都可以。
问题在于,该行的末尾有一个奇怪的不可见符号。我将代码粘贴到记事本++中,并看到了以下内容:enter image description here
因此,只需删除它,编译就会成功。