-2
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

Mig82
  • 4,856
  • 4
  • 40
  • 63
  • 1
    Need to see more code, how is this being called. First impression is that you are calling on something that is not ready yet. – thekevshow Jan 20 '19 at 22:04
  • 2
    You are using latitude and longitude from static method getURL() - as such both of them need to be static, are they? – Worthless Jan 20 '19 at 22:05
  • Possible duplicate of [What is the difference between a static method and a non-static method?](https://stackoverflow.com/questions/3903537/what-is-the-difference-between-a-static-method-and-a-non-static-method) – Progman Jan 20 '19 at 22:09
  • Please add the declaration of your class starting with `public class Locator` and the declaration of the `latitude`, `longitude` and `mContex`, as well as the compilation error you're seeing. – Mig82 Jan 20 '19 at 22:15

2 Answers2

1

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.

Antoniossss
  • 31,590
  • 6
  • 57
  • 99
0

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;
}
Mig82
  • 4,856
  • 4
  • 40
  • 63