Android Cast 应用程序中的“GoogleApiClient 尚未连接”异常
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21831224/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"GoogleApiClient is not connected yet" exception in Cast application
提问by joaomgcd
I'm developing an Android application that casts content to Chromecast.
Sometimes in my com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacksimplementation in the onConnectedmethod, I get a
我正在开发一个将内容投射到 Chromecast 的 Android 应用程序。有时在我com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks的onConnected方法实现中,我得到一个
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.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.
这似乎只有在我之前已经连接到 GoogleApiClient 并且第二次连接时才会发生。在两次调用之间,我使用以下代码与 api 客户端断开连接。
My guess is that this is a bug. Am I correct? Since I'm in the onConnectedmethod, the GoogleApiClient should already be connected.
我的猜测是这是一个错误。我对么?由于我在onConnected方法中,因此应该已经连接了 GoogleApiClient。
What can I do to get around it? Should I just wait for a while until the GoogleApiClient is really connected?
我能做些什么来解决它?我应该等待一段时间直到 GoogleApiClient 真正连接吗?
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:
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 has this code:
connectionCallback 有这个代码:
@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.
提前致谢。
回答by mattm
Google APIs for Android > GoogleApiClient
适用于 Android 的 Google API > 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.
无论状态如何,您都应该在 Activity 的 onCreate(Bundle) 方法中实例化一个客户端对象,然后在 onStart() 中调用 connect() 并在 onStop() 中调用 disconnect()。
The implementation of the GoogleApiClientappears 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.
的实现GoogleApiClient似乎只为单个实例设计。最好只在 中实例化一次onCreate,然后使用单个实例执行连接和断开连接。
I would guess that only one GoogleApiClientcan be actually be connected, but multiple instances are receiving the onConnectedcallback.
我猜GoogleApiClient实际上只能连接一个,但是多个实例正在接收onConnected回调。
In your case it's probably fine to not call connectin onStart, but only in onRouteAdded.
在你的情况下,不调用可能没connect问题onStart,但只调用onRouteAdded.
I think this issue is very similar to Fatal Exception: java.lang.IllegalStateException GoogleApiClient is not connected yet
我认为这个问题与Fatal Exception: java.lang.IllegalStateException GoogleApiClient is not connected yet非常相似
回答by Pratik Butani
Have you declare following <meta>tag
您是否声明了以下<meta>标签
<application ...>
...
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
...
</application>
I just forgot to write so may you also stuck with this reason.
我只是忘记写了,所以可能你也坚持这个原因。
Thank you.
谢谢你。
回答by Drez
From showcase app (Googlecast Github Sample CastHelloText-android) receiver app is launched onRouteSelected (not onRouteAdded as you are doing in your code). I would try to change that. In case it does not work, I would add log lines in every method related to connection & session, and see what is happening.
从展示应用程序(Googlecast Github Sample CastHelloText-android)接收器应用程序启动 onRouteSelected (而不是您在代码中所做的 onRouteAdded )。我会尝试改变这一点。如果它不起作用,我会在与连接和会话相关的每个方法中添加日志行,看看发生了什么。
Another tip: I have had crash with stopping the application (in case chromecast device is physically plugged out from power). Solution is to putCast.CastApi.stopApplication(apiClient);inside if (apiClient.isConnected()).
另一个提示:我在停止应用程序时崩溃了(以防 chromecast 设备从电源物理上拔掉)。解决办法是放在Cast.CastApi.stopApplication(apiClient);里面if (apiClient.isConnected())。
回答by Deepak
It seems like you are calling GoogleApiClient before it is connected
似乎您在连接之前调用了 GoogleApiClient
move this line in onCreate()
在 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();
}
Hope it helps you.
希望对你有帮助。

