Android – “GoogleApiClient尚未连接”异常在Cast应用程序中

前端之家收集整理的这篇文章主要介绍了Android – “GoogleApiClient尚未连接”异常在Cast应用程序中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一款向Chromecast投放内容Android应用.
有时在我的com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks实现中,在onConnected方法中,我得到一个
java.lang.IllegalStateException: GoogleApiClient is not connected yet.

例外.

这是堆栈跟踪:

FATAL EXCEPTION: main
 Process: com.joaomgcd.autocast,PID: 13771
 java.lang.IllegalStateException: GoogleApiClient is not connected yet.
    at com.google.android.gms.internal.eg.a(Unknown Source)
    at com.google.android.gms.common.api.GoogleApiClient.b(Unknown Source)
    at com.google.android.gms.cast.Cast$CastApi$a.launchApplication(Unknown Source)
    at com.joaomgcd.autocast.media.MediaConnectionCallbacks.onConnected(MediaConnectionCallbacks.java:37)
    at com.google.android.gms.internal.dx.b(Unknown Source)
    at com.google.android.gms.common.api.GoogleApiClient.bn(Unknown Source)
    at com.google.android.gms.common.api.GoogleApiClient.f(Unknown Source)
    at com.google.android.gms.common.api.GoogleApiClient$2.onConnected(Unknown Source)
    at com.google.android.gms.internal.dx.b(Unknown Source)
    at com.google.android.gms.internal.dx.bT(Unknown Source)
    at com.google.android.gms.internal.dw$h.b(Unknown Source)
    at com.google.android.gms.internal.dw$h.b(Unknown Source)
    at com.google.android.gms.internal.dw$b.bR(Unknown Source)
    at com.google.android.gms.internal.dw$a.handleMessage(Unknown Source)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5017)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
    at dalvik.system.NativeStart.main(Native Method)

这只是在我之前已经连接到GoogleApiClient并且连接了第二次的时候才会发生.在两个呼叫之间,我从api客户端断开与下面的代码.

我的猜测是这是一个bug.我对么?由于我使用onConnected方法,所以GoogleApiClient应该已经连接了.

我能做些什么来解决它?我应该等一会儿,直到GoogleApiClient真的连接好吗?

我在做这个服务,这里是相关的位:

服务开始时:

mMediaRouter.addCallback(mMediaRouteSelector,mediaCallback,MediaRouter.CALLBACK_FLAG_PERFORM_ACTIVE_SCAN);

mediaCallback有以下代码

@Override
    public void onRouteAdded(MediaRouter router,RouteInfo route) {
        super.onRouteAdded(router,route);
        if (route.getDescription().equals("Chromecast")) {
            ...
            mSelectedDevice = com.google.android.gms.cast.CastDevice.getFromBundle(route.getExtras());

            ...
                castClientListener = new CastListener(context,apiClient);

                Cast.CastOptions.Builder apiOptionsBuilder = Cast.CastOptions.builder(mSelectedDevice,castClientListener);
                ...
                apiClient.set(new GoogleApiClient.Builder(context).addApi(Cast.API,apiOptionsBuilder.build()).addConnectionCallbacks(connectionCallback).addOnConnectionFailedListener(new MediaConnectionFailedListener(context)).build());
                apiClient.get().connect();
        }

    }

connectionCallback有以下代码

@Override
    public void onConnected(final Bundle arg0) {
        ...
            Cast.CastApi.launchApplication(apiClient,UtilAutoCast.CHROMECAST_APP_ID,false).setResultCallback(connectionCallback);
        ...
    }

上面的代码是崩溃发生的部分.

当我停止服务时,我运行这段代码

if (mMediaRouter != null) {
    mMediaRouter.removeCallback(mediaCallback);
    mMediaRouter = null;
}
if (apiClient != null) {
    Cast.CastApi.stopApplication(apiClient);
    if (apiClient.isConnected()) {
        apiClient.disconnect();
        apiClient = null;
    }
}

提前致谢.

解决方法

https://developer.android.com/reference/com/google/android/gms/common/api/GoogleApiClient.html

You should instantiate a client object in your Activity’s onCreate(Bundle) method and then call connect() in onStart() and disconnect() in onStop(),regardless of the state.

GoogleApiClient的实现仅出现在一个实例上.最好在onCreate中仅实例化一次,然后使用单个实例执行连接和断开连接.

我猜测只有一个GoogleApiClient可以实际连接,但是多个实例正在接收onConnected回调.

在你的情况下,不要在onStart中调用connect,但只能在onRouteAdded中.

我觉得这个问题和Fatal Exception: java.lang.IllegalStateException GoogleApiClient is not connected yet非常相似

原文链接:https://www.f2er.com/android/311697.html

猜你在找的Android相关文章