【问题标题】:non-static method getDeviceList() cannot be referenced from a static context不能从静态上下文引用非静态方法 getDeviceList()
【发布时间】:2020-03-16 02:15:42
【问题描述】:

运行脚本时,我收到错误“无法从静态上下文引用非静态方法 getDeviceList()” - 如何使该方法成为静态方法?

private static String getUsbDeviceAddress(String selection) {
        String address = selection;

        if (android.os.Build.VERSION.SDK_INT > 21) {

                HashMap<String, UsbDevice> deviceList = UsbManager.getDeviceList();

                for (UsbDevice device : deviceList.values()) {
                    if (device != null) {
                        String dsn = device.getSerialNumber();

                        if ((dsn != null) && !dsn.isEmpty()) {
                            if (selection.equalsIgnoreCase(dsn)) {
                                address = device.getDeviceName();
                                break;
                            }
                        }
                    }
                }
        }

        return address;
    }

【问题讨论】:

  • 您不能从静态方法块中调用非静态方法,因此您可以从 getUsbDeviceAddress() 方法中删除 static 关键字或创建 getDeviceList() 静态方法
  • @vikassingh 你能告诉我你将如何创建一个 getDeviceList static 吗?

标签: java android methods


【解决方案1】:

getDeviceList() 方法不是静态的。你需要像这样声明

   public static HashMap<String, UsbDevice>  getDeviceList(){}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-22
    • 2023-03-31
    • 2013-04-05
    相关资源
    最近更新 更多