bucky bucky - 1 year ago 108
Android Question

How to fix "None-static method 'getSystemService' can not be referenced from a static context error?

I am trying to add network speed tester functionality to my app . But I am stuck at the error. Please fix ..

java file

package com.buckydroid.app.droidcpu.tools;

import android.content.Context;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

public class network {
public wifispeed(Context context){
WifiManager wifiManager = Context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo != null) {
Integer linkSpeed = wifiInfo.getLinkSpeed(); //measured using WifiInfo.LINK_SPEED_UNITS


Answer Source

getSystemService is not a static method, which means it can only be called on your object of that class, whereas a static method can be invoked on the class without needing to create an instance of it.

You're trying to call the method on the Context class instead of your instance of Context (the context object you have passed to your method) here:

 public   wifispeed(Context ***context***)

To fix, replace the capital letter C in context to a small c to use the context object you've passed in to your wifispeed method:

WifiManager wifiManager = context.getSystemService(Context.WIFI_SERVICE);

I'm specifying the changing of the letter case incase it doesn't appear obvious that one character has changed in the line of code I've provided.

Some info on the keyword static here so you can understand the error better: https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download