gpt4 book ai didi

android - Google Cast SDK3 Android 示例应用程序在低于 5.0 的设备上崩溃

转载 作者:行者123 更新时间:2023-11-29 14:45:42 24 4
gpt4 key购买 nike

我尝试了 Google Cast Android 示例应用程序,但在低于 5.0 的设备上崩溃了。任何人都知道为什么?下面是我的崩溃日志:

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.没有加一

最佳答案

最新的 Cast SDK 发生了变化,导致它与旧版本的 Google Play 服务不兼容(崩溃)。不幸的是,当使用带有过时 GPS 的最新 Cast SDK(或在模拟器上)时,即使是 Cast 示例应用程序也会崩溃。该问题已在此处讨论:https://github.com/googlecast/CastVideos-android/issues/12

解决方案是在初始化任何 cast 组件之前检查 Google Play 服务版本,包括迷你 Controller (即您不能只将迷你 Controller fragment 放入您的 xml 布局文件中 - 您必须动态膨胀它,或者有两个布局文件 - 一个有迷你 Controller ,一个没有迷你 Controller )。

检查GPS版本的代码:

GoogleApiAvailability apiAvailability = GoogleApiAvailability.getInstance();
int resultCode = apiAvailability.isGooglePlayServicesAvailable(context);
isGPSAvailable = (resultCode == ConnectionResult.SUCCESS);

如果结果不是ConnectionResult.SUCCESS,请不要初始化您的MiniControllerFragment,也不要访问CastContext

另外,请记住,使用 new MiniControllerFragment() 实例化 MiniControllerFragment不可能的。您必须从 xml 中扩充它,否则您将得到 NullPointerException

MiniControllerFragment 有两种充气方式:

  1. 创建单独的 xml 布局文件并在 Activity.onCreate 中扩充适当的文件:

    setContentView(isGPSAvailable ? R.layout.activity_main_with_controller : R.layout.activity_main_without_controller);
  2. 在您的布局中创建指向 MiniControllerFragmentViewStub 并仅在您有播放服务时对其进行膨胀。

Activity 布局:

<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:

<?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"/>

在您的 Activity onCreate() 中的代码:

ViewStub miniControllerStub = (ViewStub) findViewById(R.id.cast_minicontroller);
if (isGPSAvailable) {
miniControllerStub.inflate();
}

我更喜欢 ViewStub 方法,因为它不会重复您的布局。

关于android - Google Cast SDK3 Android 示例应用程序在低于 5.0 的设备上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39313189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com