gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 06:53:42 26 4
gpt4 key购买 nike

我正在尝试让 google 登录正常工作,但我遇到了这个错误:

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

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

最佳答案

问题是您正在将 Activity 转换为 OnConnectionFailedListener,但它没有实现该接口(interface)。

要么使 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();

关于java - GoogleApiClient.Builder.enableAutoManage() 给我一个错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39298927/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com