5

Trying to follow basic setup as per Google's guides:

mGoogleApiClient = new GoogleApiClient.Builder(this)
        .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
        .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
        .build();

The second "this" for the OnConnectionFailedListener fails

Wrong 2nd argument type.

Found: '... .HomeScreen', required: 'com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener' less...

enableAutoManage (FragmentActivity, com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener) in Builder cannot be applied to (HomeScreen, ... .HomeScreen)

So I tested this by changing the class extend from AppCompatActivity to FragmentActivity and it worked fine.

I'm not sure how to implement a listener to satisfy manually, and since AppCompatActivity extends FragmentActivity, I'm very confused as to what is going on in this case to debug it.

Further, the class has AppCompatActivity dependencies, so I'm not sure how to proceed in setting up the API.

This is follow on work to setting up the FacebookSDK, which I got working, so I was kind of hoping they had a similarly functioning "button", and that it may work in a similar manner, but the equivalent SDK initialization seems to be hiccuping at this stage.

Any direction would be welcomed.

Also, for clarification, I only need to be able to authenticate with Google, where as tagging this post with the API suggests that it is for Google Play Services, which is beyond the scope of what I require, so if I can just axe this portion, that'd be fine.

Community
  • 1
  • 1
Captain Prinny
  • 459
  • 8
  • 23
  • 1
    Read the error message. Wrong **second** argument type. Click on the underlined text, press Alt+Enter and select Make HomeScreen implement OnConnectionFailedListener. – Eugen Pechanec Feb 10 '16 at 03:05
  • See https://developers.google.com/identity/sign-in/android/sign-in and https://github.com/googlesamples/google-services/blob/master/android/signin/app/src/main/java/com/google/samples/quickstart/signin/SignInActivity.java – Eugen Pechanec Feb 10 '16 at 03:11
  • @EugenPechanec That's what the quoted text is, it's the entire thing. It does not offer any information, and the only hangup is that I'm not using a FragmentActivity, and that is the code from the first link. I'll attempt to implement the second bit of code next session as a standalone activity instead of integrating it. – Captain Prinny Feb 10 '16 at 03:57
  • 2
    You're focusing on the wrong part. `enableAutomanage` takes two parameters. 1) HomeScreen which extends AppCompatActivity which extends FragmentActivity, so there's no problem. 2) OnConnectionFailedListener. Your activity does not implement this interface. Your IDE should underline the second argument as error. Select the word and do what I mentioned in my first comment. – Eugen Pechanec Feb 10 '16 at 04:04
  • How can it not implement the interface when its parent class does? – Captain Prinny Feb 11 '16 at 00:35
  • Exactly. It can't and It doesn't. – Eugen Pechanec Feb 11 '16 at 08:53
  • Then why does FragmentActivity not need to implement it to be passed to the function as "this"? You've not answered the question, only moved it somewhere else. I've tested it and you can simply supply the FragmentActivity as "this" to the constructor for argument 2 without implementing anything, but it's child class (AppCompatActivity) does not allow this. Why. – Captain Prinny Feb 15 '16 at 23:08
  • That's simply not possible. Unless there's another class named `FragmentActivity` which implements `OnConnectionFailedListener` inside your current package. Check your imports if you're using `android.support.v4.app.FragmentActivity` or some alien class pretending to be standard `FragmentActivity`. While at it Ctrl+Click at `FragmentActivity` and check its source that it does not implement `OnConnectionFailedListener`. support-v4 library does not depend on play-services so it can't use its interfaces. – Eugen Pechanec Feb 15 '16 at 23:36
  • Can't explain it, but it's a v4.app.FagmentActivity, and it doesn't complain about the code. – Captain Prinny Feb 25 '16 at 04:05

2 Answers2

11
mGoogleApiClient = new GoogleApiClient.Builder(this)
    .enableAutoManage(this ,(OnConnectionFailedListener) this )
    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
    .build();

Simply adding a cast worked for me

Pomagranite
  • 696
  • 5
  • 11
  • 1
    sorry, it failed, at runtime it raises an exception : cannot be cast to com.google.android.gms.common.api.GoogleApiClient$OnConnectionFailedListener – danisupr4 Jan 19 '17 at 14:03
  • 2
    does the class implement googleapiclient? AppCompatActivity is used so if someone uses an old phone the app still works. FragmentActivity is a root activity that can have fragments plugged into it. I believe in newer versions any activity can use fragments. Sounds like maybe a version conflict in your build.gradle possibly and for that the gradle plugin manages version numbers https://github.com/spring-gradle-plugins/dependency-management-plugin strange this would be a runtime error – Pomagranite Feb 11 '17 at 03:37
0

1.implement OnConnectionFailedListener

public class LoginActivity extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener {

2.override

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult){

}

3.your error disappear :)

googleApiClient = new GoogleApiClient.Builder(this)
        .enableAutoManage(this ,  this)
        .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
        .build();
Energy
  • 940
  • 13
  • 20