"尚未连接&QUOT GoogleApiClient;除了在演员的应用演员、QUOT、GoogleApiClient

2023-09-13 01:29:51 作者:只身赴地狱

我开发一个Android应用程序,蒙上内容的Chromecast。 有时在onConnected方法我com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks实现,我收到了

I'm developing an Android application that casts content to Chromecast. Sometimes in my com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks implementation in the onConnected method, I get a

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和我连一秒钟的时间。 2调用之间,我从API客户端与下面的code断开连接。

This only seems to happen if I had already connected to the GoogleApiClient before and am connecting for a second time. Between the 2 calls I disconnect from the api client with the code below.

我的猜测是,这是一个错误。我对么?由于我在onConnected方法,该GoogleApiClient应已连接。

My guess is that this is a bug. Am I correct? Since I'm in the onConnected method, the GoogleApiClient should already be connected.

我能做些什么来解决呢?我应该等待一段时间,直到GoogleApiClient真的有联系吗?

What can I do to get around it? Should I just wait for a while until the GoogleApiClient is really connected?

我这样做是在服务和这里的相关位:

I am doing this in a service and here are the relevant bits:

在服务启动时:

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

mediaCallback有这个​​code:

mediaCallback has this code:

@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有这个​​code:

connectionCallback has this code:

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

在code以上就是崩溃发生的部分。

The code above is the part where the crash happens.

当我停止该服务运行此code:

And when I stop the service I run this code:

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

在此先感谢。

Thanks in advance.

推荐答案

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

您应该实例化一个客户端对象在活动的onCreate(束)方法,然后调用connect()中的OnStart()和断开的onStop()(),无论状态如何。

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 ,然后执行使用单个实例连接和断开。

The implementation of the GoogleApiClient appears designed for only a single instance. It's best to instantiate it only once in onCreate, then perform connections and disconnections using the single instance.

我猜想,只有一个 GoogleApiClient 可实际上可以连接,但多个实例正在接受 onConnected 回调。

I would guess that only one GoogleApiClient can be actually be connected, but multiple instances are receiving the onConnected callback.

在你的情况下,它可能是罚款,不叫连接 ONSTART ,但只有在 onRouteAdded

In your case it's probably fine to not call connect in onStart, but only in onRouteAdded.

我觉得这个问题是非常相似的致命的异常:java.lang.IllegalStateException GoogleApiClient尚未连接

I think this issue is very similar to Fatal Exception: java.lang.IllegalStateException GoogleApiClient is not connected yet