java.lang.UnsupportedOperationException: Can't convert to color: type=0x1

若如初见. 提交于 2019-12-01 06:06:30

问题


I've been following throught the myfirstapp guide on Android developers training but I've come across a problem where they do not properly explain how to define colors.

They mention that to create a custom theme, you can declare your text colors as such:

themes.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- the theme applied to the application or activity -->
    <style name="CustomActionBarTheme"
        parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="android:textColor">@style/MyActionBarTitleText</item>
        <item name="android:actionBarTabTextStyle">@style/MyActionBarTabText</item>
    </style>

    <!-- ActionBar styles -->
    <style name="MyActionBar"
        parent="@android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
        <item name="android:background">@drawable/actionbar_background</item>
    </style>

    <!-- ActionBar title text -->
    <style name="MyActionBarTitleText"
        parent="@android:style/TextAppearance.Holo.Widget.ActionBar.Title">
        <item name="android:textColor">@color/actionbar_text</item>
    </style>

    <!-- ActionBar tabs text styles -->
    <style name="MyActionBarTabText"
        parent="@android:style/Widget.Holo.ActionBar.TabText">
        <item name="android:textColor">@color/actionbar_text</item>
    </style>
</resources>

They do not mention how to specify @color/actionbar_text, but common sense (and some googling) indicates that a colors.xml file is required in the values package:

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="actionbar_text">#ff355689</color>
</resources>

However, when trying to run the app, it gives an error:

Process: com.example.myfirstapp, PID: 25997
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myfirstapp/com.example.myfirstapp.MainActivity}: java.lang.UnsupportedOperationException: Can't convert to color: type=0x1

If I remove the line from colors.xml, it errors when it can't find the color reference. Im relatively certain my code is correct, can anyone see any errors?

EDIT

I just wanted to note that I am actually using slightly different syntax for the themes.xml file as the tutorial ones dont compile. The tutorial uses @style/Widget.Holo.ActionBar.TabText, which I found was actually a property of android, so I needed to use @android:style/Widget.Holo.ActionBar.TabText instead.


回答1:


If I'm not mistaken that code means Android found a reference when it expected a color value or failed to convert reference to color. Looking at your code this line stands out

<item name="android:textColor">@style/MyActionBarTitleText</item>

Despite you can have a reference in textColor I'm not sure you can set it as a style.

So try to reference your color directly

<item name="android:textColor">@color/actionbar_text</item>


来源:https://stackoverflow.com/questions/26958179/java-lang-unsupportedoperationexception-cant-convert-to-color-type-0x1

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