2017-10-18 29 views
1

我正試圖將我的應用投射到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

回答

0

對於任何遇到此問題的人(可能在升級舊項目時),請不要忘記將您的活動超級課程從Activity更改爲AppCompatActivity