GoogleApiClient is not yet connected, even though onConnected was called and I am creating my GoogleApiClient in onCreate
I looked at this question and answer here: GoogleApiClient is throwing “GoogleApiClient is not connected yet” AFTER onConnected function getting called
Because it seems similar to what I experienced, but it is not. The problem for this user is that they declared their api client in the onStart() method, and I created mine in the onCreate() method, as the answer suggests. However, I still get the same error.
Here is the code for these three methods:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Butter knife creates the variables */
ButterKnife.bind(this);
/* Startup location services */
locationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
.setInterval(10 * 1000) // 10 seconds, in milliseconds
.setFastestInterval(1 * 1000); 1 second, in milliseconds
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
progressBar.setVisibility(View.INVISIBLE);
refreshImageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getForecast();
}
});
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
Log.i("Connected!!!", "WERE CONNECTED");
}
@Override
protected void onResume() {
super.onResume();
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
resumeLocationUpdates();
}
private void resumeLocationUpdates() {
Log.i("RESUMING", "RESUMING LOCATION UPDATES");
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
}
This is what the logger shows, which confuses me because it shows connected but the app crashes saying not connected….
-14 02:25:13.582 25885-25885/? I/Connected!!!: WERE CONNECTED
11-14 02:25:13.582 25885-25885/? I/RESUMING: RESUMING LOCATION UPDATES
11-14 02:25:13.582 25885-25885/? D/AndroidRuntime: Shutting down VM
11-14 02:25:13.583 25885-25885/? E/AndroidRuntime: FATAL EXCEPTION: main
11-14 02:25:13.583 25885-25885/? E/AndroidRuntime: Process: lpadron.me.weatherly, PID: 25885
11-14 02:25:13.583 25885-25885/? E/AndroidRuntime: java.lang.RuntimeException: Unable to resume activity {lpadron.me.weatherly/lpadron.me.weatherly.MainActivity}: java.lang.IllegalStateException: GoogleApiClient is not connected yet.
Solution
Your problem is in the onResume
logic:
@Override
protected void onResume() {
super.onResume();
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
resumeLocationUpdates();
}
private void resumeLocationUpdates() {
Log.i("RESUMING", "RESUMING LOCATION UPDATES");
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
}
The call to mGoogle ApiClient.connect()
is asynchronous. It returns before the connection completes, and you request a location update before the client connects. You need to move the requestLocationUpdates
call to the GoogleApiClient.onConnected
callback. After this event, your client is connected.
@Override
protected void onResume() {
super.onResume();
if (!mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
}
@Override
public void onConnected(Bundle bundle) {
resumeLocationUpdates();
}