GoogleApiClient.Builder.enableAutoManage()给了我一个错误

时间:2016-09-02 18:45:47

标签: java android

我正在尝试谷歌登录工作,我遇到了这个错误:

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

this错误,因为它需要OnConnectionFailedListener个对象,而不是Activity

所以我把它投到OnConncetionFailedListener

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

除去了红色的波浪形。但是当我运行应用程序时,应用程序崩溃,我得到的错误是:

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)

这基本上说我无法将Activity投射到OnConncetionFailedListener

我该怎么做才能纠正这个问题?

2 个答案:

答案 0 :(得分:4)

问题是您将Activity转换为OnConnectionFailedListener,但实施该接口。

使FragmentActivity实现OnConnectionFailedListener(在这种情况下可以省略显式强制转换)或子类OnConnectionFailedListener并传递该类的实例而不是Activity

最简单的解决方案可能是使用匿名内部类:

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();

答案 1 :(得分:2)

您需要一个实现OnConnectionFailedListener接口的类。有几种方法可以做到这一点:

  1. 将您的活动更改为MyActivity extends FragmentActivity implements OnConnectionFailedListener

  2. 创建新课程MyConnectionFailedListener implements OnConnectionFailedListener

  3. 直接在代码中定义匿名内部类:

    mGoogleApiClient = new GoogleApiClient.Builder(this)
        .enableAutoManage(this /* FragmentActivity */,
            new OnConnectionFailedListener() {
                // methods go here
            }
        .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
        .build();
    
  4. 现在为OnConnectionFailedListener声明的所有方法提供代码。您关注的教程很可能使用了第一种方法。就个人而言,如果方法很少,我更喜欢第三种,每种方法只有几行(少于5行)。如果OnConnectionFailedListener的代码太复杂,我将使用选项2,为新类提供描述性名称。