我在使用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)