What is static context variables

71 views Asked by At
public Locator(Context mContext){
    getLocation();
    this.mContext = mContext;
}

public void setLatitude ( String lat ){
    this.latitude = lat;
}

public String getLatitude ( ){
    return latitude;
}

public void setLongitude ( String lon ){
    this.longitude = lon;
}

public String getLongitude ( ){
    return longitude;
}

public void getLocation ( ){
    LocationManager lm = (LocationManager)mContext.getSystemService ( Context.LOCATION_SERVICE ); 
    Location location = lm.getLastKnownLocation ( LocationManager.GPS_PROVIDER );
    longitude = String.valueOf(location.getLongitude());
    latitude = String.valueOf(location.getLatitude());
}

public static String getURL(){
    return "api.openweathermap.org/data/2.5/weather?lat=" + latitude + "&lon=" + longitude + "APPID=" + APPID;
}

Both the latitude and longitude variables give me the static-context error and also in the calling function. I've tried making them static variables but no luck. Any ideas?

In another part of the code I have but no matter what I do I get a static-context error somewhere:

final String url = getApiUrlFromAreaId(areaId);

static String getApiUrlFromAreaId ( String areaId ){
    return URL + areaId;
}

No my programming is not up to par. Please bear with me

2

There are 2 answers

0
Antoniossss On

You got

public static String getURL()

which means that this method can be called without using class instance. As a consequence, everything that is used in that method, must be static as well (if not passed as argument).

I can only assume that either latitude,longitude or appId are not static. Either make them static as well, or remove static qualifier from getUrl.

0
Mig82 On

Assuming that the declaration of the latitude and longitude variables looks like this:

public class Locator{
   private String latitude; //Notice this var is not static.
   private String longitude; //Notice this var is not static.
}

and that you aim to call this from another class like so:

Locator loc = new Locator(someContext);
String url = loc.getURL();

then you have to declare the getURL method as non-static, meaning it can be invoked on a variable, and that the latitude and longitude variables it uses internally to compose the URL are the ones of said instance. So declare it like this:

public String getURL(){
   return "api.openweathermap.org/data/2.5/weather?" +
      "lat=" + latitude + //This instance's latitude
      "&lon=" + longitude + //This instance's longitude
      "APPID=" + APPID;
}

Now, if on the other hand your intention is to call this like so:

Locator loc = new Locator(someContext);
String url = Locator.getURL(loc);

Then notice here that getURL is a static method of the class, and NOT a method of an instance of the class. If this is your aim, then declare getURL like so:

public static String getURL(Locator loc){
   return "api.openweathermap.org/data/2.5/weather?" +
      "lat=" + loc.getLatitude() + //The latitude of instance loc
      "&lon=" + loc.getLongitude() + //The longitude of instance loc
      "APPID=" + APPID;
}