英文:
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 > 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;
}
答案1
得分: 0
getDeviceList() 方法不是静态的。您需要像这样声明:
public static HashMap<String, UsbDevice> getDeviceList(){}
英文:
getDeviceList() method not static.You need to declare like
public static HashMap<String, UsbDevice> getDeviceList(){}
专注分享java语言的经验与见解,让所有开发者获益!
评论