“GoogleApiClient尚未连接”Cast应用程序中的例外
我正在开发一个将内容投射到Chromecast的Android应用程序。 有时我在onConnected
方法com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks
实现,我得到一个“GoogleApiClient尚未连接”Cast应用程序中的例外
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;
}
}
在此先感谢。
从showcase应用程序(https://github.com/googlecast/CastHelloText-android)接收器应用程序启动路由选择(而不是像你在你的代码中添加路由添加)。我会尽力改变这一点。如果它不起作用,我会在与连接&会话相关的每种方法中添加日志行,并查看发生了什么。
另一个提示:我已经崩溃了停止应用程序(以防chromecast设备物理插上电源)。解决方法是将Cast.CastApi.stopApplication(apiClient);
放入if (apiClient.isConnected())
之内。
感谢您的回答,但我在后台服务中运行我的应用程序,所以我确实需要连接到添加了路由的Chromecast。在这种情况下,用户不会手动选择路线。 :) – joaomgcd
https://developer.android.com/reference/com/google/android/gms/common/api/GoogleApiClient.html
您应该实例在活动的onCreate(捆绑)方法的客户对象,然后调用connect()的调用onStart()和断开()中的onStop(),无论状态如何。
GoogleApiClient
的实现似乎只针对单个实例设计。最好在onCreate
中仅实例化一次,然后使用单个实例执行连接和断开连接。
我猜想只有一个GoogleApiClient
可以实际连接,但多个实例正在接收onConnected
回调。
在你的情况下,它可能是罚款的onStart
不叫connect
,但只有在onRouteAdded
。
我觉得这个问题是非常相似的Fatal Exception: java.lang.IllegalStateException GoogleApiClient is not connected yet
也可以使用.enableAutoManage(this/* FragmentActivity * /,this/* OnConnectionFailedListener * /)来处理connect()和disconnect()等等。 – Codeversed
你申报以下<meta>
标签
<application ...>
...
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
...
</application>
我只是忘了写,可能你也坚持了这个原因。
谢谢。
不丹,它不工作....我试过但仍然坠毁 –
好像你在呼唤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();
}
希望它可以帮助你。
你可以添加一些代码吗? – Zeus
第二次连接?你从第一个断开了吗?需要更多的代码来查看发生了什么。 –
请告诉我们你在哪里连接GoogleApiClient ... –