【问题标题】:"GoogleApiClient is not connected yet" exception in Cast applicationCast 应用程序中的“GoogleApiClient 尚未连接”异常
【发布时间】:2014-03-16 21:00:50
【问题描述】:

我正在开发一个将内容投射到 Chromecast 的 Android 应用程序。 有时在onConnected 方法中的com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks 实现中,我得到一个

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 客户端的连接。

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

提前致谢。

【问题讨论】:

  • 你能添加一些代码吗?
  • 第二次连接?你和第一个断开了吗?需要更多代码来查看发生了什么。
  • 请告诉我们您连接 GoogleApiClient 的位置...

标签: android google-cast chromecast


【解决方案1】:

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

另一个提示:我在停止应用程序时发生了崩溃(以防 chromecast 设备物理上断电)。解决办法是把Cast.CastApi.stopApplication(apiClient);放在if (apiClient.isConnected())里面。

【讨论】:

  • 感谢您的回答,但我在后台服务中运行我的应用程序,所以我真的需要在添加的路由上连接到我的 Chromecast。在这种情况下,用户不会手动选择路线。 :)
【解决方案2】:

Google APIs for Android > GoogleApiClient

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

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

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

在您的情况下,在onStart 中不调用connect 可能很好,但只能在onRouteAdded 中调用。

我觉得这个问题和Fatal Exception: java.lang.IllegalStateException GoogleApiClient is not connected yet很像

【讨论】:

  • 从示例中也可以使用 .enableAutoManage(this /* FragmentActivity /, this / OnConnectionFailedListener */),它负责 connect() 和 disconnect()似乎是这样。
【解决方案3】:

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

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

我只是忘了写,所以你也可以坚持这个原因。

谢谢。

【讨论】:

    【解决方案4】:

    您似乎在连接之前调用了 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();
        }
    

    希望对你有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-11-09
      • 1970-01-01
      相关资源
      最近更新 更多