为什么我得到java.lang.IllegalArgumentException:背景不能是半透明的:#0当使用MediaRouteButton?

问题描述:

我正试图将我的应用投射到Chromecast。我已经开始添加一个按钮,遵循https://developers.google.com/cast/docs/android_sender_integrate指南。为什么我得到java.lang.IllegalArgumentException:背景不能是半透明的:#0当使用MediaRouteButton?

我已经加入了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.xmlapplication项:

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

瓦莱斯/ 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> 

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

值-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构建。

+0

你能告诉我@颜色/黑色值吗? – Sunny

+0

'#ff000000'我可以换成别的东西,已经尝试过很少的东西了。 – Krystian

+0

尝试'#000000'。即没有alpha通道。 – Sunny

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