Why am I getting java.lang.IllegalArgumentException: background can not be translucent: #0 when using MediaRouteButton?

给你一囗甜甜゛ 提交于 2020-01-24 09:14:06

问题


I'm trying to cast my app to Chromecast. I've started with adding a button, following the https://developers.google.com/cast/docs/android_sender_integrate guide.

After I have added the MediaRouteButton, I get:

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) 

I have searched SO, and have found another, similar question, where a commenter asked to add background tag to the xml entry of MediaRouteButton. Here's my layout 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>

(I have two layouts nested because it's used programmatically, anyway I have already tried to move the MediaRouteButton higher and it didn't help).

I've also seen some tips regarding theme, here's my AndroidManifest.xml application entry:

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

Looks to me like everything should work, but I still get the damn error.

I'm using Android 26 to build.


回答1:


If you are directly instantiating the MediaRouteChooserDialog (aka. doing a bit more of a customized implementation for chromecast), and you don't want to use an AppCompat theme derivative as your app theme, you can do this:

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

This passes in the default AppCompat.NoActionBar theme for the dialog to use. You can pass whatever theme you want as long as it's a descendant of AppCompat.

Works for at least in the androidx, v7-28, and v7-28 support libraries. Not sure about older versions.




回答2:


To anyone who runs into this (probably while upgrading an old project), don't forget to change your activity super class from Activity to AppCompatActivity.




回答3:


Make sure not to override the theme in your activity style parameter:

In the android manifest the application tag should have:

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

and in the activity tag:

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



回答4:


To calculate controller color the primaryColor from your current Activity theme is used. And you have to use only fully opaque color as primaryColor to show MediaRouteButton.

Please see below the source code:

    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;


来源:https://stackoverflow.com/questions/46811254/why-am-i-getting-java-lang-illegalargumentexception-background-can-not-be-trans

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!