Cast应用程序中的“GoogleApiClient尚未连接”异常

时间:2014-02-17 14:08:28

标签: android google-cast chromecast

我正在开发一个将内容投射到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并且第二次连接,这似乎只会发生。在2次调用之间,我使用下面的代码与api客户端断开连接。

我的猜测是这是一个错误。我对么?由于我使用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;
    }
}

提前致谢。

4 个答案:

答案 0 :(得分:7)

Google APIs for Android > GoogleApiClient

  

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

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

我猜想实际上只能连接一个GoogleApiClient,但多个实例正在接收onConnected回调。

在您的情况下,可能无法在connect中拨打onStart,但仅限于onRouteAdded

我认为此问题与Fatal Exception: java.lang.IllegalStateException GoogleApiClient is not connected yet

非常相似

答案 1 :(得分:5)

您是否声明了以下 <meta> 标记

<application ...> 
... 
    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
... 
</application>

我只是忘了写,所以你也可能坚持这个理由。

谢谢。

答案 2 :(得分:2)

从展示应用程序(Googlecast Github Sample CastHelloText-android)接收器应用程序在路径选择上启动(而不是像在代码中那样在路上添加)。我会尝试改变这一点。如果它不起作用,我会在与连接和放大器相关的每个方法中添加日志行。会议,看看发生了什么。

另一个提示:我在停止应用程序时遇到了崩溃(以防chromecast设备从电源中拔出)。解决方案是将Cast.CastApi.stopApplication(apiClient);放入if (apiClient.isConnected())

答案 3 :(得分: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();
    }

希望它对你有所帮助。

相关问题