简体   繁体   English

Android 工作室,花栗鼠,蓝牙,Java

[英]Android Studio, Chipmunk, Bluetooth, Java

I am new to Stackoverflow.我是 Stackoverflow 的新手。

I get the following error and do not understand why:我收到以下错误,但不明白为什么:

incompatible types: cannot be converted to Context不兼容的类型:无法转换为 Context

Code snippet:代码片段:

           if (BluetoothDevice.ACTION_FOUND.equals(action)) {
                // Get the BluetoothDevice object from the Intent
                BluetoothDevice device = intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE);
                // Add the name and address to an array adapter to show in a ListView
                if (PackageManager.PERMISSION_GRANTED != ActivityCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT)) {
                    // TODO: Consider calling
                    //    ActivityCompat#requestPermissions
                    // here to request the missing permissions, and then overriding
                    //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                    //                                          int[] grantResults)
                    // to handle the case where the user grants the permission. See the documentation
                    // for ActivityCompat#requestPermissions for more details.
                    return;
                }

The error line did not show up: The error line is.错误行没有显示:错误行是。 if (PackageManager.PERMISSION_GRANTED,= ActivityCompat.checkSelfPermission(this. Manifest.permission.BLUETOOTH_CONNECT)) { if (PackageManager.PERMISSION_GRANTED,= ActivityCompat.checkSelfPermission(this.Manifest.permission.BLUETOOTH_CONNECT)) {

I have permissions set in my.xml file.我在 my.xml 文件中设置了权限。 I am very new to Android Studio!我对 Android Studio 很陌生!

Event Log output: C:\Users\jsla3009\AndroidStudioProjects\Test_Bluetooth\app\src\main\java\com\example\test_bluetooth\MainActivity.java:97: error: incompatible types: cannot be converted to Context if (PackageManager.PERMISSION_GRANTED.= ActivityCompat,checkSelfPermission(this. Manifest.permission:BLUETOOTH_CONNECT)) { ^ C.\Users\jsla3009\AndroidStudioProjects\Test_Bluetooth\app\src\main\java\com\example\test_bluetooth\MainActivity:java. Event Log output: C:\Users\jsla3009\AndroidStudioProjects\Test_Bluetooth\app\src\main\java\com\example\test_bluetooth\MainActivity.java:97: error: incompatible types: cannot be converted to Context if (PackageManager.PERMISSION_GRANTED .= ActivityCompat,checkSelfPermission(this.Manifest.permission:BLUETOOTH_CONNECT)) { ^ C.\Users\jsla3009\AndroidStudioProjects\Test_Bluetooth\app\src\main\java\com\example\test_bluetooth\MainActivity:Z93F7425FEZ407B338 uses or overrides a deprecated API: Recompile with -Xlint.deprecation for details: C.\Users\jsla3009\AndroidStudioProjects\Test_Bluetooth\app\src\main\java\com\example\test_bluetooth\MainActivity:java. uses or overrides a deprecated API: Recompile with -Xlint.deprecation for details: C.\Users\jsla3009\AndroidStudioProjects\Test_Bluetooth\app\src\main\java\com\example\test_bluetooth\MainActivity:java. uses unchecked or unsafe operations: Recompile with -Xlint.unchecked for details;使用未经检查或不安全的操作:使用 -Xlint.unchecked 重新编译以获取详细信息; Some messages have been simplified: recompile with -Xdiags:verbose to get full output一些消息已被简化:使用 -Xdiags:verbose 重新编译以获得完整的 output

this always references the object you are currently in. this总是引用您当前所在的 object。

Your error means that you are trying to pass something other than a Context as your first argument in checkSelfPermission , which means that this is not a Context .您的错误意味着您试图传递除Context以外的其他内容作为checkSelfPermission中的第一个参数,这意味着this不是Context

  1. Check that MainActivity extends Context or some subclass of it, eg AppCompatActivity .检查MainActivity是否扩展了Context或它的某个子类,例如AppCompatActivity
  2. Check that your code is not inside of an anonymous class .检查您的代码是否不在匿名 class中。 If it is, use MainActivity.this instead of this to clarify you mean to pass the MainActivity instance and not the instance of your anonymous class.如果是,请使用MainActivity.this而不是this来澄清您的意思是传递 MainActivity 实例而不是匿名 class 的实例。

If that doesn't help, please share your entire class!如果这没有帮助,请分享您的整个班级!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM