Non-static variable cannot be referenced from a static context?

Simone Asinelli

i'm a n00b in Android Coding, today I wanna try to use the location service.

I set up a simple class and a simple main, just to have longitude and latitude.

But when I try to call the construction for retrive the long and latitude Android Studio pop out the error:

"Error:(33, 16) error: non-static variable latitude cannot be referenced from a static context"

This is my location Class

public class AppLocationManager implements LocationListener {

    private LocationManager locationManager;
    private String latitude;
    private String longitude;
    private Criteria criteria;
    private String provider;

    public AppLocationManager(Context context) {
        locationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);
        criteria = new Criteria();
        criteria.setAccuracy(Criteria.ACCURACY_FINE);
        provider = locationManager.getBestProvider(criteria, true);
        locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,1,0,this);
        setMostRecentLocation(locationManager.getLastKnownLocation(provider));
    }

    private void setMostRecentLocation(Location lastKnownLocation) {

    }

    public String getLatitude() {
        return latitude;
    }

    public String getLongitude() {
        return longitude;
    }

    /*
     * (non-Javadoc)
     *
     * @see
     * android.location.LocationListener#onLocationChanged(android.location.
     * Location)
     */
    @Override
    public void onLocationChanged(Location location) {
        double lon = (double) (location.getLongitude());/// * 1E6);
        double lat = (double) (location.getLatitude());// * 1E6);

//      int lontitue = (int) lon;
//      int latitute = (int) lat;
        latitude = lat + "";
        longitude = lon + "";

    }

    /*
     * (non-Javadoc)
     *
     * @see
     * android.location.LocationListener#onProviderDisabled(java.lang.String)
     */
    @Override
    public void onProviderDisabled(String arg0) {
        // TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     *
     * @see
     * android.location.LocationListener#onProviderEnabled(java.lang.String)
     */
    @Override
    public void onProviderEnabled(String arg0) {
        // TODO Auto-generated method stub

    }

    /*
     * (non-Javadoc)
     *
     * @see android.location.LocationListener#onStatusChanged(java.lang.String,
     * int, android.os.Bundle)
     */
    @Override
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) {
        // TODO Auto-generated method stub

    }

}
ADM

To access non-static properties and method you will Object of that class. To learn about read this so thread :

 AppLocationManager appLocationManager=new AppLocationManager(context);
String latitude=appLocationManager.getLatitude();
String longitude=appLocationManager.getLongitude();

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

non-static class cannot be referenced from a static context

From Java

Java Generics: non-static type variable T cannot be referenced from a static context

From Java

"Non-static variable this cannot be referenced from a static context" when creating an object

From Java

Why do I get "non-static variable this cannot be referenced from a static context"?

From Java

non static method cannot be referenced from a static context

From Java

"non-static variable this cannot be referenced from a static context"?

From Java

java : non-static variable cannot be referenced from a static context Error

From Dev

java: non-static variable this cannot be referenced from a static context

From Dev

Rectangle.java:35: error: non-static variable this cannot be referenced from a static context

From Dev

Print at jTextArea using .setText returns error non-static variable cannot be referenced from static context

From Dev

error: non-static variable scan cannot be referenced from a static context in Java

From Dev

Non static variable cannot be referenced from static context java

From Dev

non-static variable s cannot be referenced from a static context

From Dev

"Non-static method cannot be referenced from static context" error

From Dev

non static variable this cannot be referenced from a static context

From Dev

non-static variable cannot be referenced from a static context java

From Dev

non-static method getIntent() cannot be referenced from a static context

From Dev

Java JTextField and non-static variable cannot be referenced from a static context

From Dev

Non-static variable filepath cannot be referenced from a static context

From Dev

Java Language | Error :- non-static variable scan cannot be referenced from a static context

From Dev

non-static variable this cannot be referenced from a static context issue when instantiating a new object

From Dev

Non-static edit() cannot be referenced from a static context

From Dev

non static setGravity cannot be referenced from static context

From Dev

non-static variable cannot be referenced from static context [JAVA]

From Dev

Error: Non-static variable super cannot be referenced from a static context >>but i use static keyword

From Dev

Java Blackjack program creates error: non-static variable this cannot be referenced from a static context

From Dev

non-static variable this cannot be referenced from a static context Dormitory dormitory = new Dormitory();

From Dev

Non-static method getSocketFactory cannot be referenced from a static context

From Dev

Can't call constructor of inner class in a static context -- "non-static variable this cannot be referenced from a static context"

Related Related

  1. 1

    non-static class cannot be referenced from a static context

  2. 2

    Java Generics: non-static type variable T cannot be referenced from a static context

  3. 3

    "Non-static variable this cannot be referenced from a static context" when creating an object

  4. 4

    Why do I get "non-static variable this cannot be referenced from a static context"?

  5. 5

    non static method cannot be referenced from a static context

  6. 6

    "non-static variable this cannot be referenced from a static context"?

  7. 7

    java : non-static variable cannot be referenced from a static context Error

  8. 8

    java: non-static variable this cannot be referenced from a static context

  9. 9

    Rectangle.java:35: error: non-static variable this cannot be referenced from a static context

  10. 10

    Print at jTextArea using .setText returns error non-static variable cannot be referenced from static context

  11. 11

    error: non-static variable scan cannot be referenced from a static context in Java

  12. 12

    Non static variable cannot be referenced from static context java

  13. 13

    non-static variable s cannot be referenced from a static context

  14. 14

    "Non-static method cannot be referenced from static context" error

  15. 15

    non static variable this cannot be referenced from a static context

  16. 16

    non-static variable cannot be referenced from a static context java

  17. 17

    non-static method getIntent() cannot be referenced from a static context

  18. 18

    Java JTextField and non-static variable cannot be referenced from a static context

  19. 19

    Non-static variable filepath cannot be referenced from a static context

  20. 20

    Java Language | Error :- non-static variable scan cannot be referenced from a static context

  21. 21

    non-static variable this cannot be referenced from a static context issue when instantiating a new object

  22. 22

    Non-static edit() cannot be referenced from a static context

  23. 23

    non static setGravity cannot be referenced from static context

  24. 24

    non-static variable cannot be referenced from static context [JAVA]

  25. 25

    Error: Non-static variable super cannot be referenced from a static context >>but i use static keyword

  26. 26

    Java Blackjack program creates error: non-static variable this cannot be referenced from a static context

  27. 27

    non-static variable this cannot be referenced from a static context Dormitory dormitory = new Dormitory();

  28. 28

    Non-static method getSocketFactory cannot be referenced from a static context

  29. 29

    Can't call constructor of inner class in a static context -- "non-static variable this cannot be referenced from a static context"

HotTag

Archive