Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
172 views
in Technique[技术] by (71.8m points)

android - "GoogleApiClient is not connected yet" exception in Cast application

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.

exception.

Here is the stack trace:

    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)

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.

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

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:

when the service starts:

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

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 has this code:

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

The code above is the part where the crash happens.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Google APIs for Android > GoogleApiClient

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.

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.

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

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

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...