"GoogleApiClient is not connected yet"异常在Cast应用中出现

12

我正在开发一个将内容投放到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;
    }
}

提前致谢。


1
你能添加一些代码吗? - Zeus
第二次连接?你已经断开了第一个连接吗?需要更多你这边的代码来看发生了什么。 - Ali Naddaf
请告诉我们您在哪里连接GoogleApiClient... - IgorGanapolsky
4个回答

7

Google APIs for Android > GoogleApiClient

您应该在Activity的onCreate(Bundle)方法中实例化客户端对象,然后在onStart()中调用connect(),在onStop()中调用disconnect(),无论状态如何。

GoogleApiClient的实现似乎只设计为单个实例。最好在onCreate中仅实例化一次,然后使用单个实例执行连接和断开连接。

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

在您的情况下,不在onStart中调用connect,而只在onRouteAdded中调用可能是可以的。

我认为这个问题非常类似于 致命异常:java.lang.IllegalStateException GoogleApiClient尚未连接


此外,从这些示例中,您可以使用.enableAutoManage(this /* FragmentActivity /, this / OnConnectionFailedListener */)来处理connect()和disconnect(),或者看起来是这样的。 - Codeversed

5
你是否声明了以下的 <meta> 标签?
<application ...> 
... 
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
... 
</application>

我只是忘记写了,所以你也可能因此卡住。

谢谢。


2

来自展示应用程序(Googlecast Github 示例 CastHelloText-android)的接收器应用程序在onRouteSelected上启动(而不是在您的代码中所做的onRouteAdded)。我建议更改这一点。如果不起作用,我会在与连接和会话相关的每个方法中添加日志行,并查看发生了什么。

另一个提示:我曾经因停止应用程序(如果chromecast设备从电源中拔出)而崩溃。解决方案是将Cast.CastApi.stopApplication(apiClient);放置在if (apiClient.isConnected())内部。


谢谢您的回答,但我正在后台服务中运行我的应用程序,因此我确实需要在路由添加时连接到我的Chromecast。在这种情况下,用户不会手动选择路由。 :) - joaomgcd

0

看起来你在连接前调用了 GoogleApiClient

把这行代码移到 onCreate() 中

// First we need to check availability of play services
    if (checkPlayServices()) {

        // Building the GoogleApi client
        //buildGoogleApiClient();
        try {
            mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this).addOnConnectionFailedListener(this).addApi(LocationServices.API).build();

            mGoogleApiClient.connect();
        }catch (IllegalStateException e)
        {
            Log.e("IllegalStateException", e.toString());
        }
        createLocationRequest();
    }

希望它能对你有所帮助。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接