0

我无法获得UsbManager的工作实例。

根据 Android USB guide,我应该可以通过以下任一方式访问它:

UsbManager manager = UsbManager.getInstance(this);

或者

UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);

但两者似乎都不起作用。第一个给出错误

无法解析 UsbManager 中的方法“getInstance”。

后者给出:

必须是以下之一:Context.POWER_SERVICE、Context.WINDOW_SERVICE、Context.LAYOUT_INFLATER_SERVICE、Context.ACCOUNT_SERVICE、Context.ACTIVITY_SERVICE、Context.ALARM_SERVICE、Context.NOTIFICATION_SERVICE、Context.ACCESSIBILITY_SERVICE、android.content.Context.CAPTIONING_SERVICE、Context.KEYGUARD_SERVICE、Context .LOCATION_SERVICE,Context.SEARCH_SERVICE,Context.SENSOR_SERVICE,android.content.Context.STORAGE_SERVICE,android.content.Context.STORAGE_STATS_SERVICE,Context.WALLPAPER_SERVICE,Context.VIBRATOR_SERVICE,Context.CONNECTIVITY_SERVICE,android.content.Context.IPSEC_SERVICE,android.content .Context.VPN_MANAGEMENT_SERVICE,android.content.Context.NETWORK_STATS_SERVICE,Context.WIFI_SERVICE,android.content.Context.WIFI_AWARE_SERVICE,android.content.Context.WIFI_P2P_SERVICE,android.content.Context.WIFI_RTT_RANGING_SERVICE,android.content。Context.NSD_SERVICE,Context.AUDIO_SERVICE,android.content.Context.FINGERPRINT_SERVICE,android.content.Context.BIOMETRIC_SERVICE,android.content.Context.MEDIA_ROUTER_SERVICE,Context.TELEPHONY_SERVICE,android.content.Context.TELEPHONY_SUBSCRIPTION_SERVICE,android.content.Context。 CARRIER_CONFIG_SERVICE,android.content.Context.EUICC_SERVICE,android.content.Context.TELECOM_SERVICE,Context.CLIPBOARD_SERVICE,Context.INPUT_METHOD_SERVICE,android.content.Context.TEXT_SERVICES_MANAGER_SERVICE,android.content.Context.TEXT_CLASSIFICATION_SERVICE,android.content.Context.APPWIDGET_SERVICE, android.content.Context.DROPBOX_SERVICE,android.content.Context.DEVICE_POLICY_SERVICE,android.content.Context.UI_MODE_SERVICE,android.content.Context.DOWNLOAD_SERVICE,android.content.Context.NFC_SERVICE,android.content.Context.BLUETOOTH_SERVICE,android。content.Context.USB_SERVICE,android.content.Context.LAUNCHER_APPS_SERVICE,android.content.Context.INPUT_SERVICE,android.content.Context.DISPLAY_SERVICE,android.content.Context.USER_SERVICE,android.content.Context.RESTRICTIONS_SERVICE,android.content。 Context.APP_OPS_SERVICE,android.content.Context.ROLE_SERVICE,android.content.Context.CAMERA_SERVICE,android.content.Context.PRINT_SERVICE,android.content.Context.CONSUMER_IR_SERVICE,android.content.Context.TV_INPUT_SERVICE,android.content.Context。 USAGE_STATS_SERVICE,android.content.Context.MEDIA_SESSION_SERVICE,android.content.Context.BATTERY_SERVICE,android.content.Context.JOB_SCHEDULER_SERVICE,android.content.Context.MEDIA_PROJECTION_SERVICE,android.content.Context.MIDI_SERVICE,android.content.Context.HARDWARE_PROPERTIES_SERVICE, android.content.Context.SHORTCUT_SERVICE,安卓。content.Context.SYSTEM_HEALTH_SERVICE,android.content.Context.COMPANION_DEVICE_SERVICE,android.content.Context.CROSS_PROFILE_APPS_SERVICE

必须是以下之一:Context.POWER_SERVICE、Context.WINDOW_SERVICE、Context.LAYOUT_INFLATER_SERVICE、Context.ACCOUNT_SERVICE、Context.ACTIVITY_SERVICE、Context.ALARM_SERVICE、Context.NOTIFICATION_SERVICE、Context.ACCESSIBILITY_SERVICE、android.content.Context.CAPTIONING_SERVICE、Context.KEYGUARD_SERVICE、Context .LOCATION_SERVICE,Context.SEARCH_SERVICE,Context.SENSOR_SERVICE,android.content.Context.STORAGE_SERVICE,android.content.Context.STORAGE_STATS_SERVICE,Context.WALLPAPER_SERVICE,Context.VIBRATOR_SERVICE,Context.CONNECTIVITY_SERVICE,android.content.Context.IPSEC_SERVICE,android.content .Context.VPN_MANAGEMENT_SERVICE,android.content.Context.NETWORK_STATS_SERVICE,Context.WIFI_SERVICE,android.content.Context.WIFI_AWARE_SERVICE,android.content.Context.WIFI_P2P_SERVICE,android.content.Context.WIFI_RTT_RANGING_SERVICE,android.content。Context.NSD_SERVICE,Context.AUDIO_SERVICE,android.content.Context.FINGERPRINT_SERVICE,android.content.Context.BIOMETRIC_SERVICE,android.content.Context.MEDIA_ROUTER_SERVICE,Context.TELEPHONY_SERVICE,android.content.Context.TELEPHONY_SUBSCRIPTION_SERVICE,android.content.Context。 CARRIER_CONFIG_SERVICE,android.content.Context.EUICC_SERVICE,android.content.Context.TELECOM_SERVICE,Context.CLIPBOARD_SERVICE,Context.INPUT_METHOD_SERVICE,android.content.Context.TEXT_SERVICES_MANAGER_SERVICE,android.content.Context.TEXT_CLASSIFICATION_SERVICE,android.content.Context.APPWIDGET_SERVICE, android.content.Context.DROPBOX_SERVICE,android.content.Context.DEVICE_POLICY_SERVICE,android.content.Context.UI_MODE_SERVICE,android.content.Context.DOWNLOAD_SERVICE,android.content.Context.NFC_SERVICE,android.content.Context.BLUETOOTH_SERVICE,android。content.Context.USB_SERVICE,android.content.Context.LAUNCHER_APPS_SERVICE,android.content.Context.INPUT_SERVICE,android.content.Context.DISPLAY_SERVICE,android.content.Context.USER_SERVICE,android.content.Context.RESTRICTIONS_SERVICE,android.content。 Context.APP_OPS_SERVICE,android.content.Context.ROLE_SERVICE,android.content.Context.CAMERA_SERVICE,android.content.Context.PRINT_SERVICE,android.content.Context.CONSUMER_IR_SERVICE,android.content.Context.TV_INPUT_SERVICE,android.content.Context。 USAGE_STATS_SERVICE,android.content.Context.MEDIA_SESSION_SERVICE,android.content.Context.BATTERY_SERVICE,android.content.Context.JOB_SCHEDULER_SERVICE,android.content.Context.MEDIA_PROJECTION_SERVICE,android.content.Context.MIDI_SERVICE,android.content.Context.HARDWARE_PROPERTIES_SERVICE, android.content.Context.SHORTCUT_SERVICE,安卓。content.Context.SYSTEM_HEALTH_SERVICE,android.content.Context.COMPANION_DEVICE_SERVICE,android.content.Context.CROSS_PROFILE_APPS_SERVICE

我正在使用android.hardware.usb.UsbAccessoryandandroid.hardware.usb.UsbManager库作为参考。我的 build.gradle 文件的相关部分如下:

buildscript {
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:4.2.2'
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.5.21" // needed for YuvToRgbConverter
    }
}

subprojects {
    group = 'com.example'
    version = getGitVersion()
    println "version = " + getGitVersion()
    ext.gitVersion = scmTag()
    ext.versionNamespace = 'abcvlib'
    ext.versionString = sprintf("%s%s", versionNamespace, version)

    repositories {
        mavenCentral()
        mavenLocal()
        maven { url 'https://jitpack.io' }
        google()
        jcenter()
    }

    apply plugin: 'maven-publish'
    apply plugin: 'signing'

    if (gradle.ext.androidLibs.any{name -> name == project.name}){
        apply plugin: 'com.android.library'
        apply plugin: "kotlin-android" // needed for YuvToRgbConverter
    } else if (gradle.ext.apps.any{name -> name == project.name}){
        apply plugin: 'com.android.application'
        dependencies {
            implementation(project(":abcvlib"))
        }
    }

    dependencies {
        implementation 'androidx.annotation:annotation:1.2.0'
        implementation 'androidx.constraintlayout:constraintlayout:2.1.0'
        implementation "androidx.lifecycle:lifecycle-livedata:2.3.1"
        implementation "androidx.lifecycle:lifecycle-viewmodel:2.3.1"
        implementation "androidx.lifecycle:lifecycle-common-java8:2.3.1"
        implementation 'androidx.appcompat:appcompat:1.3.1'
        implementation 'androidx.activity:activity:1.3.1'
        implementation "androidx.fragment:fragment:1.3.6"
        implementation "androidx.core:core-ktx:1.6.0"
        implementation 'androidx.navigation:navigation-fragment:2.3.5'
        implementation 'androidx.navigation:navigation-ui:2.3.5'

        // CameraX core library using the camera2 implementation
        implementation "androidx.camera:camera-camera2:1.1.0-alpha07"
        // If you want to additionally use the CameraX Lifecycle library
        implementation "androidx.camera:camera-lifecycle:1.1.0-alpha07"
        // If you want to additionally use the CameraX View class
        implementation "androidx.camera:camera-view:1.0.0-alpha27"
        // If you want to additionally use the CameraX Extensions library
        implementation "androidx.camera:camera-extensions:1.0.0-alpha27"

        implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.5.21"
        implementation "com.google.flatbuffers:flatbuffers-java:1.12.0"
        implementation 'com.google.android.material:material:1.4.0'
        implementation 'com.google.zxing:core:3.4.1'
        implementation 'io.github.nishkarsh:android-permissions:2.0.54'
    }

    android{
        compileOptions {
            sourceCompatibility(JavaVersion.VERSION_1_8)
            targetCompatibility(JavaVersion.VERSION_1_8)
        }
        defaultConfig {
            compileSdkVersion = 30
            buildToolsVersion = '30.0.2'
            minSdkVersion 24
            targetSdkVersion 30
        }
        ndkVersion = "21.0.6113669"
    }
}

编辑:

我尝试制作一个新的 Android 项目,似乎UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);语法在那里有效,所以我尝试了以下步骤:

  1. 将此空白“应用程序”模块复制/粘贴到问题仍然存在的原始项目中。
  2. 覆盖项目和模块 build.gradle 文件。以“大多数是以下之一:...”开始的第二个错误仍然存​​在。
  3. 这开始看起来像缓存问题。我使android studio缓存无效并重新启动。问题仍然存在。
  4. 我从工作目录中删除了所有内容,并将新空白项目的内容复制到现有项目中。问题仍然存在。
  5. 最后,我再次删除了所有内容并且git reset --hard HEAD. 问题消失了。

根据给出的错误,任何关于这种情况如何发生和/或如何在未来避免它的见解,都有望在未来对其他人有所帮助。

4

0 回答 0