McFiddlyWiddly
McFiddlyWiddly

Reputation: 581

GoogleApiClient.Builder.enableAutoManage() giving me an error

I'm trying to get google sign in working, and I ran into this error:

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

this is wrong because it wants an OnConnectionFailedListener object, not an Activity.

So I cast it to OnConncetionFailedListener:

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

Which removes the red squigglys. But when I run the app, the app crashes, and the error I get is:

09-02 13:01:37.937    2403-2403/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.android.googlelogin, PID: 2403
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.googlelogin/com.example.android.googlelogin.MainActivity}: java.lang.ClassCastException: com.example.android.googlelogin.MainActivity cannot be cast to com.google.android.gms.common.api.GoogleApiClient$OnConnectionFailedListener
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
     Caused by: java.lang.ClassCastException: com.example.android.googlelogin.MainActivity cannot be cast to com.google.android.gms.common.api.GoogleApiClient$OnConnectionFailedListener
            at com.example.android.googlelogin.MainActivity.onCreate(MainActivity.java:35)
            at android.app.Activity.performCreate(Activity.java:6237)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
            at android.app.ActivityThread.-wrap11(ActivityThread.java)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:148)
            at android.app.ActivityThread.main(ActivityThread.java:5417)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Which basically says that I cannot cast an Activity to an OnConncetionFailedListener.

What do I do to correct this?

Upvotes: 1

Views: 5746

Answers (2)

earthw0rmjim
earthw0rmjim

Reputation: 19417

The problem is you are casting your Activity to OnConnectionFailedListener, but it does not implement that interface.

Either make FragmentActivity implement OnConnectionFailedListener (the explicit cast can be omitted in this case), or subclass OnConnectionFailedListener and pass an instance of that class instead of your Activity.

The simpliest solution might be to use an anonymous inner class:

mGoogleApiClient = new GoogleApiClient.Builder(this)
        .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
            @Override
            public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
                // connection failed, should be handled
            }
        })
        .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
        .build();

Upvotes: 4

Code-Apprentice
Code-Apprentice

Reputation: 83517

You need a class which implements the OnConnectionFailedListener interface. There several ways to do this:

  1. Change your activity to MyActivity extends FragmentActivity implements OnConnectionFailedListener.

  2. Create a new class MyConnectionFailedListener implements OnConnectionFailedListener.

  3. Define an anonymous inner class directly in your code:

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

Now provide code for all methods declared by OnConnectionFailedListener. Most likely the tutorial you are following used the first approach. Personally, I prefer the third if there are few methods and each method has only a few lines (less than 5). If the code for OnConnectionFailedListener is too complex, I will use option 2 with a descriptive name for the new class.

Upvotes: 2

Related Questions