Reputation: 87
I've been trying to figure out how to solve this problem for a long time now, but I can't find any good information on what is going wrong.
The code originates from: http://developer.android.com/google/auth/api-client.html
So first I build the GoogleApiClient object and then onStart() try to connect it. The connection fails and I end up inside "onConnectionFailed()"..
Here I've printed the result and this is what I get: "SIGN_IN_REQUIRED"
As that error has a solution "result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR)" will be called.
Here is where the program suddenly crashes.
Any help would be appriciated I'm quite new to android development so go easy on me!
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.content.IntentSender.SendIntentException;
import android.util.Log;
import android.view.Menu;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.plus.Plus;
public class MainActivity extends Activity implements
ConnectionCallbacks, OnConnectionFailedListener {
/* Client used to interact with Google APIs. */
private GoogleApiClient mGoogleApiClient;
// Request code to use when launching the resolution activity
private static final int REQUEST_RESOLVE_ERROR = 1001;
// Bool to track whether the app is already resolving an error
private boolean mResolvingError = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(Plus.API, null)
.addScope(Plus.SCOPE_PLUS_LOGIN)
.build();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
if (mResolvingError) {
// Already attempting to resolve an error.
return;
} else if (result.hasResolution()) {
Log.d("MyApp",result.toString());
try {
mResolvingError = true;
result.startResolutionForResult(this, REQUEST_RESOLVE_ERROR);
} catch (SendIntentException e) {
// There was an error with the resolution intent. Try again.
mGoogleApiClient.connect();
}
} else {
// Show dialog using GooglePlayServicesUtil.getErrorDialog()
//showErrorDialog(result.getErrorCode());
mResolvingError = true;
}
}
@Override
public void onConnected(Bundle connectionHint) {
}
@Override
public void onConnectionSuspended(int cause) {
mGoogleApiClient.connect();
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_RESOLVE_ERROR) {
mResolvingError = false;
if (resultCode == RESULT_OK) {
// Make sure the app is not already connected or attempting to connect
if (!mGoogleApiClient.isConnecting() && !mGoogleApiClient.isConnected()) {
mGoogleApiClient.connect();
}
}
}
}
}
Upvotes: 1
Views: 7168
Reputation: 111
In the onCreate
method try to:
super.onCreate(savedInstanceState);
.addScope(Plus.SCOPE_PLUS_LOGIN)
or .setScopes("PLUS_LOGIN")
as was in my caseIf this does not help, try to import and run great example project from SDK samples:
<android-sdk-folder>/extras/google/google_play_services/
See https://developers.google.com/+/mobile/android/getting-started
Upvotes: 1