Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Instance variables not being updated not sure why. maybe its not being run on the main thread as it is a callback...

This is the code that gets called when the GoogleApiClient gets created. All this does it retrieve the last location of the user.

onConnected.java
      //omitted code

    @Override
    public void onConnected(Bundle bundle) {
        Location location = LocationServices.FusedLocationApi.getLastLocation(mApiClient);
        if (location != null) {
            mLongitude = String.valueOf(location.getLongitude());
            mLatitude = String.valueOf(location.getLatitude());
            Log.d(TAG, mLongitude + "___" + mLatitude); //this one doesn't return null
        } else {
            Log.d(TAG, "Location is null");
            LocationServices.FusedLocationApi.requestLocationUpdates(mApiClient, mLocationRequest, this);
        }
    }

This is for the google api client:

GoogleApi.java
private void buildGoogleApiClient() {
        mApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
    }

This snippet is from my onCreate() method:

buildGoogleApiClient();
Log.d(TAG, mLongitude + "___" + mLatitude); //this one returns null

Now i will explain my best of what happens. So when the code gets run, i build a google api client and the onConnected method gets called. Inside of that method, i will update the mLongitude and mLatitude variables which are instance variables. However in the log, they return as null. I was wondering why this happened so i put another log INSIDE the if statement of my onConnected() method as you can see in the first block of code and this one successfully brings back the longitude and latitude. I dont really know whats going on. I dont know why its not updating the instance variables. Or maybe it's something to do with not running on the main thread because i know that it's a callback.

Anyone have any insight to share?

Thank you

1 Answer

https://developers.google.com/android/reference/com/google/android/gms/common/api/GoogleApiClient.html#connect%28%29

"Connects the client to Google Play services. This method returns immediately, and connects to the service in the background. If the connection is successful, onConnected(Bundle) is called and enqueued items are executed. On a failure, onConnectionFailed(ConnectionResult) is called."

Since you're trying to log the location right after starting to connect, it won't have had enough time to connect to the Google API and get your location. You need to wait for the location to come through before using it.

Hey Seth,

Where exactly should i put the connect() method?