其他分享
首页 > 其他分享> > Non-static method ‘*‘ cannot be referenced from a static context (在静态上下文中不能引用非静态方法)

Non-static method ‘*‘ cannot be referenced from a static context (在静态上下文中不能引用非静态方法)

作者:互联网

在静态上下文中不能引用非静态方法
原因:直接调用了其他包内的非静态方法。
解决方法:要有实例才能调用静态方法。

原本的代码

 public void onAttach(final UsbDevice device) {
            Toast.makeText(MainActivity.this, "USB_DEVICE_ATTACHED", Toast.LENGTH_SHORT).show();
            FileUtil.writeToFile("/sdcard/Android/data/test", "cameralist.txt", String.valueOf(UsbDevice.getProductId()));
        }

修改后

 public void onAttach(final UsbDevice device) {
            Toast.makeText(MainActivity.this, "USB_DEVICE_ATTACHED", Toast.LENGTH_SHORT).show();
            FileUtil.writeToFile("/sdcard/Android/data/test", "cameralist.txt", String.valueOf(device.getProductId()));
        }

总之。。。我犯的是一个很弱智的问题。如果你没有实例化一个对象,先实例化一个。

标签:Toast,Non,referenced,静态方法,UsbDevice,实例,static,device,final
来源: https://blog.csdn.net/Dadaist_/article/details/113860720