代码之家  ›  专栏  ›  技术社区  ›  Incog

为什么没有检测到这个变量?安卓工作室Kotlin

  •  0
  • Incog  · 技术社区  · 1 年前

    在下面的代码中,无法识别“takePermission”和“bluetoothAdapter”。我一直在关注 this tutorial 这似乎有点过时,但他的变量被公认为没有问题。

    class MainActivity : ComponentActivity() {
        lateinit var bluetoothManager: BluetoothManager
        lateinit var bluetoothAdapter: BluetoothAdapter
        lateinit var takePermission: ActivityResultLauncher<String>
        lateinit var takeResultLauncher: ActivityResultLauncher<Intent>
        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            bluetoothManager = getSystemService(BLUETOOTH_SERVICE) as BluetoothManager
            bluetoothAdapter = bluetoothManager.adapter
            takePermission = registerForActivityResult(ActivityResultContracts.RequestPermission()){
                if (it){
                    val intent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
                    takeResultLauncher.launch(intent)
                } else {
                    Toast.makeText(applicationContext,"Bluetooth Permission not given",Toast.LENGTH_SHORT).show()
                }
            }
    ...
    
    @Composable
    fun Greeting() {
        Column(
            modifier = Modifier.fillMaxWidth(),
            horizontalAlignment = Alignment.CenterHorizontally,
            verticalArrangement = Arrangement.Center
        ) {
            Text("Bluetooth ON/OFF Application", fontSize = 20.sp,
                fontWeight = FontWeight.Bold, textAlign = TextAlign.Center)
            Spacer(modifier = Modifier.height(20.dp))
            OutlinedButton(onClick = {
                takePermission.launch(android.Manifest.permission.BLUETOOTH_CONNECT) //unresolved reference error
            }) {
                Text("Bluetooth On", fontSize = 30.sp,
                    fontWeight = FontWeight.Bold)
            }
            Spacer(modifier = Modifier.height(20.dp))
            OutlinedButton(onClick = {
                bluetoothAdapter.disable() //unresolved reference error
            }) {
                Text("Bluetooth Off", fontSize = 30.sp,
                    fontWeight = FontWeight.Bold)
            }
            Spacer(modifier = Modifier.height(20.dp))
        }
    }
    
    
    1 回复  |  直到 1 年前
        1
  •  0
  •   selbie    1 年前

    在黑暗中拍摄,根据观察到的凹痕图案进行猜测。缩进模式表明 Greeting 实际上不在MainActivity的类范围内。即你有 招呼 作为一个全局函数。

    我怀疑您的代码结构是这样的:

    class MainActivity : ComponentActivity() {
        lateinit var bluetoothManager: BluetoothManager
        lateinit var bluetoothAdapter: BluetoothAdapter
        lateinit var takePermission: ActivityResultLauncher<String>
        lateinit var takeResultLauncher: ActivityResultLauncher<Intent>
    
        override fun onCreate(savedInstanceState: Bundle?) {
        }
    
        ...
    
    }  // END OF MainActivity Defintition
    
    
    // Greeting is at global scope
    fun Greeting() {
    
    }
    

    您想要:

    class MainActivity : ComponentActivity() {
        lateinit var bluetoothManager: BluetoothManager
        lateinit var bluetoothAdapter: BluetoothAdapter
        lateinit var takePermission: ActivityResultLauncher<String>
        lateinit var takeResultLauncher: ActivityResultLauncher<Intent>
    
        override fun onCreate(savedInstanceState: Bundle?) {
        }
    
        fun Greeting() {
    
        }
    
    }  // END OF MainActivity Defintition
    
    

    如果这不能解决你的问题,那么我怀疑你需要发布 MVCE .