You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无需AppCompatActivity实现Chromecast的报错解决求助

问题描述

我正在开发一款用于Unity的Android插件,实现Chromecast功能。使用AppCompatActivity及@style/Theme.AppCompat.Light.NoActionBar主题时功能运行正常,但切换为该Activity会引发诸多其他问题,因此希望尽可能避免使用它。

目前应用使用@android:style/Theme.NoTitleBar.Fullscreen主题,执行代码行LinearLayout fragment1 = (LinearLayout) View.inflate(UnityPlayer.currentActivity, R.layout.fragment1, null);时抛出错误:

java.lang.IllegalArgumentException: background can not be translucent: #0

相关布局代码如下:

fragment1.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000"
    android:orientation="vertical" >

    <fragment
        android:name="com.app.FragmentButtons"
        android:id="@+id/fragment_placeholder"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </fragment>

</LinearLayout>

buttons.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/buttons_view"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:gravity="right"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <androidx.mediarouter.app.MediaRouteButton
        android:id="@+id/media_route_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:mediaRouteTypes="user"
        android:visibility="gone"
        android:background="#000000"
        />
</LinearLayout>

请问是否有无需使用AppCompatActivity即可修复该报错并实现Chromecast的方法?


解决方案

不用切换到AppCompatActivity也能解决这个问题,试试下面几种方法:

  • 自定义主题绕过半透明检查
    在styles.xml里创建继承自@android:style/Theme.NoTitleBar.Fullscreen的自定义主题,明确禁用半透明并设置不透明背景,同时适配MediaRouteButton的样式:

    <style name="CustomUnityCastTheme" parent="@android:style/Theme.NoTitleBar.Fullscreen">
        <item name="android:windowIsTranslucent">false</item>
        <item name="android:windowBackground">@android:color/black</item>
        <item name="mediaRouteButtonStyle">@style/CustomMediaRouteBtnStyle</item>
    </style>
    
    <style name="CustomMediaRouteBtnStyle" parent="android:Widget.Material.MediaRouteButton">
        <item name="background">@android:color/black</item>
    </style>
    

    然后在AndroidManifest.xml中将Unity对应的Activity主题替换为这个自定义主题。

  • 代码中强制设置不透明背景
    虽然布局里已经写了黑色背景,但可以在代码里显式设置,确保系统识别为不透明:

    LinearLayout fragment1 = (LinearLayout) View.inflate(UnityPlayer.currentActivity, R.layout.fragment1, null);
    // 强制设置根布局背景为不透明黑色
    fragment1.setBackgroundColor(UnityPlayer.currentActivity.getResources().getColor(android.R.color.black));
    // 给MediaRouteButton也强制设置背景
    MediaRouteButton mediaBtn = fragment1.findViewById(R.id.media_route_button);
    if (mediaBtn != null) {
        mediaBtn.setBackgroundColor(UnityPlayer.currentActivity.getResources().getColor(android.R.color.black));
    }
    
  • 手动初始化MediaRouter适配非AppCompat环境
    直接使用MediaRouter的API手动初始化,不需要依赖AppCompatActivity的自动适配:

    MediaRouter mediaRouter = MediaRouter.getInstance(UnityPlayer.currentActivity);
    MediaRouteButton mediaRouteBtn = findViewById(R.id.media_route_button);
    // 设置路由选择器,匹配Chromecast设备
    MediaRouteSelector selector = new MediaRouteSelector.Builder()
            .addControlCategory(MediaControlIntent.CATEGORY_LIVE_AUDIO)
            .addControlCategory(MediaControlIntent.CATEGORY_LIVE_VIDEO)
            .build();
    mediaRouteBtn.setRouteSelector(selector);
    

    同时确保自定义主题里包含基础的AppCompat属性,避免组件样式崩溃:

    <style name="CustomUnityCastTheme" parent="@android:style/Theme.NoTitleBar.Fullscreen">
        <!-- 其他属性 -->
        <item name="colorPrimary">@android:color/black</item>
        <item name="colorPrimaryDark">@android:color/black</item>
    </style>
    

内容的提问来源于stack exchange,提问作者LachoTomov

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.18 11:05:40