Google Cast SDK3 Android示例应用程序在低于5.0的设备上崩溃
我已经尝试过Google Cast Android示例应用程序,但在5.0以下的设备上崩溃了. 有人知道为什么吗?以下是我的崩溃日志:
Ive tried the Google Cast Android sample app, and is crashing for device below 5.0. Anyone has any idea why? below is my crash log:
0830 12:38:57.242:E/AndroidRuntime(16269):原因:java.lang.RuntimeException: com.google.android.gms.internal.zzsb $ zza:找不到可接受的模块.本地版本为0,远程版本为0. 0830 12:38:57.242:E/AndroidRuntime(16269):在com.google.android.gms.internal.zzni.zzbg(未知来源) 0830 12:38:57.242:E/AndroidRuntime(16269):在com.google.android.gms.internal.zzni.zza(未知来源) 0830 12:38:57.242:E/AndroidRuntime(16269):在com.google.android.gms.cast.framework.CastContext. 没有加一
0830 12:38:57.242: E/AndroidRuntime(16269): Caused by: java.lang.RuntimeException: com.google.android.gms.internal.zzsb$zza: No acceptable module found. Local version is 0 and remote version is 0. 0830 12:38:57.242: E/AndroidRuntime(16269): at com.google.android.gms.internal.zzni.zzbg(Unknown Source) 0830 12:38:57.242: E/AndroidRuntime(16269): at com.google.android.gms.internal.zzni.zza(Unknown Source) 0830 12:38:57.242: E/AndroidRuntime(16269): at com.google.android.gms.cast.framework.CastContext. 没有加一
最新的Cast SDK进行了更改,使其与旧版本的Google Play服务不兼容(崩溃). 不幸的是,当使用最新的Cast SDK和过时的GPS(或在模拟器上)时,甚至Cast示例应用程序也会崩溃. 该问题已在此处进行了讨论: https://github.com/googlecast/CastVideos-android /issues/12
There was a change in the latest Cast SDK that made it incompatible (crashing) with older versions of Google Play Services. Unfortunately, even cast sample app crashes when using latest Cast SDK with outdated GPS (or on emulators). The issue has been discussed here: https://github.com/googlecast/CastVideos-android/issues/12
解决方案是在初始化包括小型控制器在内的所有演员表组件之前检查Google Play服务版本(即,您不能只是将小型控制器片段放入xml布局文件中-您必须动态地对其进行膨胀,或者具有两个布局文件-一个带有迷你控制器的文件,另一个带有不带迷你控制器的文件.
The solution is to check Google Play Services version before initialising any cast components, including mini controller (i.e. you can't just put mini controller fragment into your xml layout file - you either have to inflate it dynamically, or have two layout files - one with, and one without your mini controller).
检查GPS版本的代码:
Code to check GPS version:
GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
int resultCode = apiAvailability.isGooglePlayServicesAvailable(context);
isGPSAvailable = (resultCode == ConnectionResult.SUCCESS);
如果结果不是ConnectionResult.SUCCESS
,请不要初始化您的MiniControllerFragment
,也不要访问CastContext
.
If the results is not ConnectionResult.SUCCESS
, don't initialise your MiniControllerFragment
and do not access CastContext
.
此外,请记住,不可能使用new MiniControllerFragment()
实例化MiniControllerFragment
.您必须从xml对其进行充气,否则将得到NullPointerException
.
Also, please keep in mind that it is not possible to instantiate MiniControllerFragment
using new MiniControllerFragment()
. You have to inflate it from xml or you will get NullPointerException
.
有两种方法可以充气MiniControllerFragment
:
-
创建单独的xml布局文件,并在
Activity.onCreate
中充入适当的文件:
setContentView(isGPSAvailable ? R.layout.activity_main_with_controller : R.layout.activity_main_without_controller);
在布局中创建指向MiniControllerFragment
的ViewStub
,并仅在有游戏服务时才对其进行充气.
Create ViewStub
in your layout pointing to MiniControllerFragment
and inflate it only when you have play services.
活动布局:
<ViewStub
android:id="@+id/cast_minicontroller"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout="@layout/cast_mini_controller_fragment"
/>
cast_mini_controller_fragment:
cast_mini_controller_fragment:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/castMiniController"
class="com.google.android.gms.cast.framework.media.widget.MiniControllerFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
您的活动onCreate()
中的代码:
ViewStub miniControllerStub = (ViewStub) findViewById(R.id.cast_minicontroller);
if (isGPSAvailable) {
miniControllerStub.inflate();
}
我更喜欢ViewStub
方法,因为它不会重复您的布局.
I prefer the ViewStub
approach as it does not duplicate your layouts.