非静态方法getDeviceList()无法从静态上下文中引用。

huangapple 未分类评论47阅读模式
英文:

non-static method getDeviceList() cannot be referenced from a static context

问题

当运行脚本时我收到错误消息"非静态方法 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;
}
英文:

When running script I receive the error "non-static method getDeviceList() cannot be referenced from a static context" - How do I make this method static?

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

        if (android.os.Build.VERSION.SDK_INT &gt; 21) {

                HashMap&lt;String, UsbDevice&gt; deviceList = UsbManager.getDeviceList();

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

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

        return address;
    }

答案1

得分: 0

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

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

getDeviceList() method not static.You need to declare like

   public static HashMap&lt;String, UsbDevice&gt;  getDeviceList(){}

huangapple
  • 本文由 发表于 2020年3月16日 10:15:42
  • 转载请务必保留本文链接:https://java.coder-hub.com/60699565.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定