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

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.

1 answer

  • answered 2022-04-16 20:20 Mouaad Abdelghafour AITALI

    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;
        }
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum