【问题标题】:Why am I getting java.lang.IllegalArgumentException: background can not be translucent: #0 when using MediaRouteButton?为什么我得到 java.lang.IllegalArgumentException: background can not be translucent: #0 when using MediaRouteButton?
【发布时间】:2018-03-30 09:23:23
【问题描述】:

我正在尝试将我的应用投射到 Chromecast。我已经开始按照https://developers.google.com/cast/docs/android_sender_integrate 指南添加一个按钮。

添加 MediaRouteButton 后,我得到:

Caused by: java.lang.IllegalArgumentException: background can not be translucent: #0
at android.support.v4.graphics.ColorUtils.calculateContrast(ColorUtils.java:93)
at android.support.v7.app.MediaRouterThemeHelper.getControllerColor(MediaRouterThemeHelper.java:88)
at android.support.v7.app.MediaRouterThemeHelper.getStyledRouterThemeId(MediaRouterThemeHelper.java:185)
at android.support.v7.app.MediaRouterThemeHelper.createThemedContext(MediaRouterThemeHelper.java:60)
at android.support.v7.app.MediaRouteButton.<init>(MediaRouteButton.java:124)
at android.support.v7.app.MediaRouteButton.<init>(MediaRouteButton.java:120)
at java.lang.reflect.Constructor.newInstance(Native Method) 
at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
at android.view.LayoutInflater.rInflate(LayoutInflater.java:838) 
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:408) 
at android.app.Activity.setContentView(Activity.java:2198) 

我搜索了 SO,并发现了另一个类似的问题,其中评论者要求将背景标签添加到 MediaRouteButton 的 xml 条目。这是我的布局 xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mdb_reader"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

   <LinearLayout
        android:id="@+id/mdb_book_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.v7.app.MediaRouteButton
            android:id="@+id/media_route_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/black"
            android:layout_weight="1"
            android:mediaRouteTypes="user"
            android:visibility="gone" />

    </LinearLayout>

</FrameLayout>

(我有两个嵌套的布局,因为它以编程方式使用,无论如何我已经尝试将 MediaRouteButton 移得更高但没有帮助)。

我还看到了一些关于主题的提示,这是我的 AndroidManifest.xml application 条目:

<application
    android:allowBackup="true"
    android:hardwareAccelerated="true"
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:background="@android:color/black"
    android:theme="@style/AppTheme">

vales/styles.xml

<resources>
    <style name="AppBaseTheme" parent="@style/Theme.AppCompat">
    </style>

    <style name="AppTheme" parent="AppBaseTheme">
    </style>

    <style name="Theme.CastVideosTheme" parent="AppBaseTheme">
        <item name="mediaRouteTheme">@style/CustomMediaRouterTheme</item>
    </style>

    <style name="CustomMediaRouterTheme" parent="Theme.MediaRouter">
        <item name="mediaRouteButtonStyle">@style/CustomMediaRouteButtonStyle</item>
    </style>

    <style name="CustomMediaRouteButtonStyle" parent="Widget.MediaRouter.Light.MediaRouteButton">
        <item name="buttonTint">@color/black</item>
    </style>
</resources>

values-v11/styles.xml

<resources>
    <style name="AppBaseTheme" parent="@style/Theme.AppCompat">
    </style>
    <style name="Theme.CastVideosTheme" parent="AppBaseTheme">
        <item name="mediaRouteTheme">@style/CustomMediaRouterTheme</item>
    </style>

    <style name="CustomMediaRouterTheme" parent="Theme.MediaRouter">
        <item name="mediaRouteButtonStyle">@style/CustomMediaRouteButtonStyle</item>
    </style>

    <style name="CustomMediaRouteButtonStyle" parent="Widget.MediaRouter.Light.MediaRouteButton">
        <item name="buttonTint">@color/black</item>
    </style>
</resources>

values-v14

<resources>
    <style name="AppBaseTheme" parent="@style/Theme.AppCompat">
    </style>
    <style name="Theme.CastVideosTheme" parent="AppBaseTheme">
        <item name="mediaRouteTheme">@style/CustomMediaRouterTheme</item>
    </style>

    <style name="CustomMediaRouterTheme" parent="Theme.MediaRouter">
        <item name="mediaRouteButtonStyle">@style/CustomMediaRouteButtonStyle</item>
    </style>

    <style name="CustomMediaRouteButtonStyle" parent="Widget.MediaRouter.Light.MediaRouteButton">
        <item name="buttonTint">@color/black</item>
    </style>
</resources>

在我看来一切都应该正常工作,但我仍然得到该死的错误。

我正在使用 Android 26 构建。

【问题讨论】:

  • 你能告诉我@color/black 的值吗?
  • &lt;color name="black"&gt;#ff000000&lt;/color&gt; 我可以换成别的,已经尝试了一些东西。
  • 试试&lt;color name="black"&gt;#000000&lt;/color&gt;。即没有 alpha 通道。
  • 试过了,同样的问题。顺便说一句,我不知道有人指定color/black 是红色的......
  • yup @Krystian 您的 colorPrimary 和其他不应包含 Alpha 通道。

标签: android xml chromecast


【解决方案1】:

如果您直接实例化MediaRouteChooserDialog(又名。为 chromecast 做更多的自定义实现),并且您不想使用 AppCompat 主题衍生品作为您的应用主题,您可以这样做:

MediaRouteChooserDialog builder = new MediaRouteChooserDialog(context, R.style.Theme_AppCompat_NoActionBar);

这会传入默认的 AppCompat.NoActionBar 主题以供对话框使用。你可以传递任何你想要的主题,只要它是 AppCompat 的后代。

至少适用于 androidx、v7-28 和 v7-28 支持库。不确定旧版本。

【讨论】:

    【解决方案2】:

    要计算控制器颜色,请使用当前 Activity 主题中的 primaryColor。而且您必须仅使用 完全不透明 颜色作为 primaryColor 来显示 MediaRouteButton。

    请看下面的源代码:

        int primaryColor = getThemeColor(context, style,
                android.support.v7.appcompat.R.attr.colorPrimary);
        if (ColorUtils.calculateContrast(COLOR_WHITE_ON_DARK_BACKGROUND, primaryColor)
                >= MIN_CONTRAST) {
            return COLOR_WHITE_ON_DARK_BACKGROUND;
        }
        return COLOR_DARK_ON_LIGHT_BACKGROUND;
    

    【讨论】:

      【解决方案3】:

      确保不要在活动样式参数中覆盖主题:

      在 android manifest 中应用标签应该有:

      <application
          **android:theme="@style/Theme.AppCompat.NoActionBar"**
           android:icon="@drawable/icon"
           android:label="@string/app_name"
           android:name="MyApplication">
      

      在活动标签中:

      <activity
          android:name=".VideoActivity"
          android:configChanges="orientation|screenSize"
          android:label="Video"
          android:theme="@style/Theme.AppCompat.NoActionBar" >
      </activity>
      

      【讨论】:

        【解决方案4】:

        对于遇到此问题的任何人(可能在升级旧项目时),不要忘记将您的活动超级类从 Activity 更改为 AppCompatActivity

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-05-06
          • 2022-01-12
          • 1970-01-01
          • 1970-01-01
          • 2022-11-25
          • 1970-01-01
          • 1970-01-01
          • 2022-12-27
          相关资源
          最近更新 更多