How to fix the problem with error "No virtual method" on android

16.3k views Asked by At

I am writing an application in android studio for android Pie. I want to get wifi info.

    @RequiresApi(api = Build.VERSION_CODES.S)
    public String getData() {       
 
        String wifiDataTest;
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int frequency = wifiInfo.getFrequency();
        int ipAddress = wifiInfo.getIpAddress();
        int speed = wifiInfo.getLinkSpeed();
        String ssid = wifiInfo.getSSID();
        int wifiStandard = wifiInfo.getWifiStandard();

        wifiDataTest = "Frequency [MHz]: " + frequency + "\nIP address: " + ipAddress
                + "\nSpeed [Mbps]: " + speed + "\nSSID: " + ssid  + "\nWifi standard: " + wifiStandard;


        return "WIFI\n" + wifiDataTest;
    }

With int wifiStandard = wifiInfo.getWifiStandard (); shows me the error:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.sensors, PID: 11040
    java.lang.NoSuchMethodError: No virtual method getWifiStandard()I in class Landroid/net/wifi/WifiInfo; or its super classes (declaration of 'android.net.wifi.WifiInfo' appears in /system/framework/framework.jar)

I have not found information about such an error anywhere. Everything before getWifiStandard (); worked fine.

2

There are 2 answers

0
Mouaad Abdelghafour AITALI On

You're trying to run your app which contains the getWifiStandard() method that exists ONLY on Android 11 or up.

Try this :

public String getData() {

        String wifiDataTest;
        WifiInfo wifiInfo = wifiManager.getConnectionInfo();
        int frequency = wifiInfo.getFrequency();
        int ipAddress = wifiInfo.getIpAddress();
        int speed = wifiInfo.getLinkSpeed();
        String ssid = wifiInfo.getSSID();
        String wifiStandard = "";
        
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.R) {
            wifiDataTest = "Frequency [MHz]: " + frequency + "\nIP address: " + ipAddress
                    + "\nSpeed [Mbps]: " + speed + "\nSSID: " + ssid + "\nWifi standard: " + wifiInfo.getWifiStandard();
        } else {
            if (wifiInfo.getLinkSpeed() <= 11) wifiStandard =  "802.b";
            else if (wifiInfo.getLinkSpeed() <= 54) wifiStandard =  "802.g";
            else if (wifiInfo.getLinkSpeed() <= 300) wifiStandard =  "802.11n";
            else if (wifiInfo.getLinkSpeed() <= 866.5) wifiStandard =  "802.11ac";
            
            wifiDataTest = "Frequency [MHz]: " + frequency + "\nIP address: " + ipAddress
                    + "\nSpeed [Mbps]: " + speed + "\nSSID: " + ssid + "\nWifi standard: " + wifiStandard;
        }
       
        return "WIFI\n" + wifiDataTest;
    }
0
Đức Lê Công On

Situtation 1: It's maybe you just reference to the function, missing ()

  • wrong example:

    fun main(callback: () -> Unit) = callback

  • correct example:

    fun main(callback: () -> Unit) = callback()

Situtation 2: the function is a Job, needed to be an Unit

  • wrong example:

    fun main(callback: () -> Unit) = MainScope().launch { callback() }

  • correct example:

    fun main(callback: () -> Unit) { MainScope().launch { callback() } }

Situtation 3: just Google & God know ;))