Android – “GoogleApiClient尚未连接”异常在Cast应用程序中

Android – “GoogleApiClient尚未连接”异常在Cast应用程序中,第1张

概述我正在开发一款向Chromecast投放内容的 Android应用. 有时在我的com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks实现中,在onConnected方法中,我得到一个 java.lang.IllegalStateException: GoogleApiClient is not connected 我正在开发一款向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.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;    }}

提前致谢.

解决方法 https://developer.android.com/reference/com/google/android/gms/common/api/GoogleApiClient.html

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.

Googleapiclient的实现仅出现在一个实例上.最好在onCreate中仅实例化一次,然后使用单个实例执行连接和断开连接.

我猜测只有一个Googleapiclient可以实际连接,但是多个实例正在接收onConnected回调.

在你的情况下,不要在onStart中调用connect,但只能在onRouteAdded中.

我觉得这个问题和Fatal Exception: java.lang.IllegalStateException GoogleApiClient is not connected yet非常相似

总结

以上是内存溢出为你收集整理的Android – “GoogleApiClient尚未连接”异常在Cast应用程序中全部内容,希望文章能够帮你解决Android – “GoogleApiClient尚未连接”异常在Cast应用程序中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1132280.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存