view raw
bucky bucky - 11 months ago 46
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


import android.content.Context;

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



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: