0

I have a class GetMyLocation that gets the current location of the user. Now I'm trying to call an object of this class from another class, ShowMap2. But I am unable to send the updated location to the new class.

I haven't included the other overrides for GetMyLocation here.

What I have:

public class ShowMap2 extends Activity{
        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_showmap);

        GetMyLocation myLocation = new GetMyLocation();


        myLat = myLocation.GetCurrentLat();
        myLon = myLocation.GetCurrentLon();

   }

This is the class that gets the location:

public class GetMyLocation extends Activity implements LocationListener {


    protected LocationManager locationManager;
    protected LocationListener locationListener;
    Location location;

    public static double myLat, myLon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

        location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);


       this.myLat = location.getLatitude();
       this.myLon = location.getLongitude();

    }


    public double GetCurrentLat(){
        return this.myLat;
    }

    public double GetCurrentLon(){
        return this.myLon;
    }

The above code returns 0.0 for both lat and lon.

I have tried every possible way I could think of, but I always end up get 0.0. I know the class itself gets my current lat and lon, but I am not able to pass it correctly. Could some please help me out here?

Thanks.

PS:

I found this thread, and tried it accordingly, but to no avail. Still zeros.

How to access this variable from an Android onCreate method?

The only difference, in the question in the link, the class is static. When I make my class static, it says: Modifier 'static' not allowed here.

My updated code:

public class GetMyLocation extends Activity implements LocationListener {

    protected LocationManager locationManager;
    protected LocationListener locationListener;
    Location location;

    static double myLat, myLon;

    double GetCurrentLat(){
        return myLat;
    }

    double GetCurrentLon(){
        return myLon;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

        location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);

        this.myLat = location.getLongitude();
        this.myLon = location.getLongitude();

    }

EDIT: Updated Code that works:

public class GetMyLocation implements LocationListener {
//public class GetMyLocation extends Activity implements LocationListener {

    private static final LatLng HAMBURG = new LatLng(43.48484521,-80.5274279);
    private GoogleMap map;

    protected LocationManager locationManager;
    protected LocationListener locationListener;
    Location location;

    private static double myLat, myLon;

    GetMyLocation(Activity activity){

        locationManager = (LocationManager) activity.getSystemService(Context.LOCATION_SERVICE);
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

        location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);

        this.myLat = location.getLatitude();
        this.myLon = location.getLongitude();

    }

    double GetCurrentLat(){
        return this.myLat;
    }

    double GetCurrentLon(){
        return this.myLon;
    }

getting the location values from ShowMap2 using:

GetMyLocation myLocation = new GetMyLocation(this);
2
  • did u send the variables via intent in secondActivity? Commented Feb 27, 2015 at 5:23
  • simply instantiation a class GetMyLocation myLocation = new GetMyLocation(); does not call methods on it `onCreate(Bundle savedInstanceState);' You need to learn how to create a new Activity Commented Feb 27, 2015 at 5:29

2 Answers 2

0

You're extending Activity in GetMyLocation. For the goal you want, remove extends Activity, and move the code from onCreate() to a suitable constructor such as GetMyLocation().

The rest of your logic is solid.

An activity is for GUI, and if you don't intend on opening a new activity with your class, don't extend Activity. For the code to work from onCreate(), you'd have to create an intent of your class, and start the activity, which would open a blank activity, because you're not setting any views in GetMyLocation.

In layman's terms, your onCreate() is never called because you're never starting the activity.

EDIT To pass a Context to our class so it runs correctly, we will define our constructor like so:

GetMyLocation(Context context) {
    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);

    location = locationManager.getLastKnownLocation(locationManager.GPS_PROVIDER);

    this.myLat = location.getLatitude();
    this.myLon = location.getLongitude();

}

And we will initialize our class like so: GetMyLocation location = new GetMyLocation(this); from within your Activity. When we reference this from our class, it points to that class, which just so happens to be an Activity satisfying our GetMyLocation(Context context) constructor. Then you can call your methods and get the expected results.

When you originally called getSystemService() you implied this which was a reference to your activity that was never started. That's also why your IDE got mad at you when you removed extends Activity from GetMyLocation(). It didn't know where to look for for getSystemService(), because the implied class this was no longer a Context.

Sign up to request clarification or add additional context in comments.

10 Comments

If I remove extend Activity, it cannot resolve getSystemService. Also I had previously moved the code from onCreate() to a contructor. But that would make the app close/crash when I ran it.
Then you must pass a Context as a parameter in your constructor. I'll add an example snippet to my answer.
That works well, thanks. Just one question, what is the Context that I should be passing from my ShowMap2 class. This is my first app in Java, so I'm sort of lost.
@fractal5, Context is what Activity extends, and getSystemService() is a method of Context. You can replace Context with Activity and in your instance, it will run exactly the same with no errors. Every Activity is a Context, but every Context is not an Activity. Just like every cat is an animal, but every animal is not a cat. If this concept doesn't make sense, google java inheritance and take a look at some of the documentation. Oh and if you feel I've helped you feel free to upvote or accept the answer.
@fractal5, also think of animals eating. All animals eat, but not every animal necessarily eats the same thing, or the same way. This is akin to inherited methods.
|
-1

If you want to pass parameters from Activity to Activity, use Bundle. If you want to get result when Activity is exiting, use startActivityForRestult when creating the new Activity. If you just want to pass parameters from Activity to another class, use the class's constructor.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.