34

I am following Google's documentation to implement Google+ Sign In feature into an app.

https://developers.google.com/+/mobile/android/getting-started

I followed each step according to the guide but got stuck in an error generated by the GoogleApiClient.Builder , I searched thoroughly but got no result. Please help me sort it out. Thank you.

Error code:

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).addApi(Plus.API, null)
            .addScope(Plus.SCOPE_PLUS_LOGIN).build();

Error Message :

   The method addConnectionCallbacks(GoogleApiClient.ConnectionCallbacks) in the type 
   GoogleApiClient.Builder is not applicable for the arguments (MainActivity)

Complete MainActivity.java code :

    package mad.project.mightysatta;

    import android.content.Intent;
    import android.content.IntentSender.SendIntentException;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v7.app.ActionBarActivity;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;

    import com.google.android.gms.common.ConnectionResult;
    import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
    import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
    import com.google.android.gms.common.api.GoogleApiClient;
    import com.google.android.gms.plus.Plus;

    public class MainActivity extends ActionBarActivity implements
    ConnectionCallbacks, OnConnectionFailedListener {

/* Request code used to invoke sign in user interactions. */
private static final int RC_SIGN_IN = 0;

/* Client used to interact with Google APIs. */
private GoogleApiClient mGoogleApiClient;

/*
 * A flag indicating that a PendingIntent is in progress and prevents us
 * from starting further intents.
 */
private boolean mIntentInProgress;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this).addApi(Plus.API, null)
            .addScope(Plus.SCOPE_PLUS_LOGIN).build();

    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

}

@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);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
    // return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    // TODO Auto-generated method stub

    if (!mIntentInProgress && result.hasResolution()) {
        try {
            mIntentInProgress = true;
            result.startResolutionForResult(this, // your activity
                    RC_SIGN_IN);
        } catch (SendIntentException e) {
            // The intent was canceled before it was sent. Return to the
            // default
            // state and attempt to connect to get an updated
            // ConnectionResult.
            mIntentInProgress = false;
            mGoogleApiClient.connect();
        }
    }

}

@Override
public void onConnected(Bundle connectionHint) {
    // TODO Auto-generated method stub

}

@Override
public void onDisconnected() {
    // TODO Auto-generated method stub

}

protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

protected void onStop() {
    super.onStop();

    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

protected void onActivityResult(int requestCode, int responseCode,
        Intent intent) {
    if (requestCode == RC_SIGN_IN) {
        mIntentInProgress = false;

        if (!mGoogleApiClient.isConnecting()) {
            mGoogleApiClient.connect();
        }
    }
}

public void onConnectionSuspended(int cause) {
    mGoogleApiClient.connect();
}

    }

In this code if I comment out .addConnectionCallbacks and .addOnConnectionFailedListener , then the error goes away. The error seems to be related with their arguments.

    mGoogleApiClient = new GoogleApiClient.Builder(this)
        //  .addConnectionCallbacks(this)
        //  .addOnConnectionFailedListener(this)
            .addApi(Plus.API, null)
            .addScope(Plus.SCOPE_PLUS_LOGIN).build();

Updated Main Activity , after replacing implements to

    GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener

MainActivity.java(Updated)

    package mad.project.mightysatta;

    import android.content.Intent;
    import android.content.IntentSender.SendIntentException;
    import android.os.Bundle;
    import android.support.v4.app.Fragment;
    import android.support.v7.app.ActionBarActivity;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;

    import com.google.android.gms.common.ConnectionResult;
    import com.google.android.gms.common.GooglePlayServicesClient;
    import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;
    import com.google.android.gms.common.GooglePlayServicesClient.OnConnectionFailedListener;
    import com.google.android.gms.common.api.GoogleApiClient;
    import com.google.android.gms.plus.Plus;

    public class MainActivity extends ActionBarActivity implements
    GooglePlayServicesClient.ConnectionCallbacks,
    GooglePlayServicesClient.OnConnectionFailedListener {

/* Request code used to invoke sign in user interactions. */
private static final int RC_SIGN_IN = 0;

/* Client used to interact with Google APIs. */
private GoogleApiClient mGoogleApiClient;

/*
 * A flag indicating that a PendingIntent is in progress and prevents us
 * from starting further intents.
 */
private boolean mIntentInProgress;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API, null)
            .addScope(Plus.SCOPE_PLUS_LOGIN).build();

    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }

}

@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);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main_activity_actions, menu);
    return super.onCreateOptionsMenu(menu);
    // return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        return rootView;
    }
}

@Override
public void onConnectionFailed(ConnectionResult result) {
    // TODO Auto-generated method stub

    if (!mIntentInProgress && result.hasResolution()) {
        try {
            mIntentInProgress = true;
            result.startResolutionForResult(this, // your activity
                    RC_SIGN_IN);
        } catch (SendIntentException e) {
            // The intent was canceled before it was sent. Return to
            // default
            // state and attempt to connect to get an updated
            // ConnectionResult.
            mIntentInProgress = false;
            mGoogleApiClient.connect();
        }
    }

}

@Override
public void onConnected(Bundle connectionHint) {
    // TODO Auto-generated method stub

}

@Override
public void onDisconnected() {
    // TODO Auto-generated method stub

}

protected void onStart() {
    super.onStart();
    mGoogleApiClient.connect();
}

protected void onStop() {
    super.onStop();

    if (mGoogleApiClient.isConnected()) {
        mGoogleApiClient.disconnect();
    }
}

protected void onActivityResult(int requestCode, int responseCode,
        Intent intent) {
    if (requestCode == RC_SIGN_IN) {
        mIntentInProgress = false;

        if (!mGoogleApiClient.isConnecting()) {
            mGoogleApiClient.connect();
        }
    }
}

public void onConnectionSuspended(int cause) {
    mGoogleApiClient.connect();
}

    }
Taha Rushain
  • 635
  • 1
  • 9
  • 26
  • Taha, I had the same problem and came up with the same solution you mentioned above. Are you using Android Studio? If so, were you able to successfully do a G+ Sign In? – Gerard Aug 11 '14 at 01:03
  • I was using ADT-Bundle, and yes it did Log-in. I think it required to have G+ app installed and logged-in in to your application's device – Taha Rushain Aug 12 '14 at 00:28

5 Answers5

72

I see in their documentation where they clearly instruct you to include this import:

import com.google.android.gms.common.GooglePlayServicesClient.ConnectionCallbacks;

However, the error being thrown is expecting a different class than GooglePlayServicesClient.ConnectionCallbacks, it's asking for GoogleApiClient.ConnectionCallbacks. Try changing your implements to use the more-qualified class name. That looks to be the only possible thing throwing the code for a loop and without the explicit qualified classname, it will default to the directly imported class name.

It's always tougher when you have to question the manual.

Edit: I mean a change like this:

public class MainActivity 
    extends ActionBarActivity 
    implements GoogleApiClient.ConnectionCallbacks,
               GoogleApiClient.OnConnectionFailedListener {
  • I replaced the implementations to GooglePlayServicesClient.ConnectionCallbacks, GooglePlayServicesClient.OnConnectionFailedListener But the problem remains. – Taha Rushain May 20 '14 at 06:29
  • Nothing , its the same error. In this code if I comment out .addConnectionCallbacks and .addOnConnectionFailedListener , then the error goes away. The error seems to be related with their arguments. mGoogleApiClient = new GoogleApiClient.Builder(this) // .addConnectionCallbacks(this) //.addOnConnectionFailedListener(this).addApi(Plus.API, null) .addScope(Plus.SCOPE_PLUS_LOGIN).build(); – Taha Rushain May 20 '14 at 06:33
  • Different code very rarely gives an exact error like other code. Can you update your question with the current MainActivity.java? Also, did you clean the project after changing? –  May 20 '14 at 06:35
  • I updated my answer based on your updated code in your question. I still recommend doing a clean after changing (never hurts!) –  May 20 '14 at 06:41
  • Thank you Jeremy , that solves the issue. I hope the API would work fine now. – Taha Rushain May 20 '14 at 06:46
  • 6
    This is correct. Documentation bug filed. Thanks. – Hounshell May 22 '14 at 17:52
  • 16
    thanks for this fix. you'd think google would test their own code. every step of implementing google play services is requiring stack overflow research to fix weird errors. I hope someday to get it fully implemented. – Androidcoder Jul 11 '14 at 19:47
  • i agree on the clean suggestion. i never had to clean my code until adding inapp purchasing, and now adding play services. if i don't clean before every single compile i get a 'please fix your errors' message with no red flags to indicate where the alleged errors are. – Androidcoder Jul 11 '14 at 19:54
  • 1
    Michael, I'm glad I'm not the only one getting tripped by the documentation. Hardly any of their samples run without extensive fixes. – Gerard Aug 11 '14 at 01:13
  • 1
    @Gerard Glad I was able to helpl. This was definitely challenging. –  Aug 11 '14 at 01:14
  • 3
    As an update, I just ran into this as well. Still not fixed - filed another documentation bug. – JasCav Sep 03 '14 at 03:06
  • @Jeremy Thanks for pointing out the problem. I would never had been able to guess. –  Nov 08 '14 at 00:51
16

I too faced the same problem, i resolved by doing the following things.

import the right ConnectionCallbacks.

here is my code:

import android.content.Context;
import android.os.Bundle;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.drive.Drive;

public class GplusLogin implements
    GoogleApiClient.ConnectionCallbacks,
    GoogleApiClient.OnConnectionFailedListener {
GoogleApiClient mGoogleApiClient;
GplusLogin(Context context){

    mGoogleApiClient = new GoogleApiClient.Builder(context)
            .addApi(Drive.API)
            .addScope(Drive.SCOPE_FILE)
            .addConnectionCallbacks((GoogleApiClient.ConnectionCallbacks) context)
            .addOnConnectionFailedListener(this)
            .build();
}

@Override
public void onConnected(Bundle bundle) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {

}

@Override
public void onConnectionSuspended(int i) {

}
}
raju
  • 1,254
  • 13
  • 18
  • 1
    Thanks, I had the same problem. I had only one (correct) ConnectionCallbacks imported, but it just crashed at .addConnectionCallbacks until I explicitly cast the context to (GoogleApiClient.ConnectionCallbacks). I was using it in a Service rather than an ACtivity, but they both extend Context anyway so not sure why it should matter. – Mick O'Hea Jul 29 '14 at 20:54
2

turns out they (google) don't follow much of the guide themselves. Use their Google Drive Android Quickstart as a reference, which they actually do mention at the top of the tutorial, but they give it the misleading name "Android Quickstart" even though it is specific to Google Drive

The note at the top of the guide: The note at the top of the guide

woojoo666
  • 7,801
  • 7
  • 45
  • 57
2

click alt enter and force android studio to implement:

GoogleApiClient.ConnectionCallbacks,

GoogleApiClient.OnConnectionFailedListener

carlos
  • 21
  • 1
0
   implements LocationListener, GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener,OnMapReadyCallback

to the Activity

Brinda Rathod
  • 2,693
  • 1
  • 20
  • 32