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

11,196

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>
Share:
11,196
RonnyKnoxville
Author by

RonnyKnoxville

Previous Owner/Operator - 8-bit Squirrel (2015 - present) Kainos Software - Software Engineer (2014 - 2015) Housebites - CTO (2013 - 2014) Inmarsat - Web Developer (2010 - 2012)

Updated on June 07, 2022

Comments

  • RonnyKnoxville
    RonnyKnoxville almost 2 years

    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?

    android studio showing error

    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.

  • RonnyKnoxville
    RonnyKnoxville over 9 years
    Absolutely correct. Sometimes you just cant see the wood through the trees. Thankyou!
  • Maurice Gavin
    Maurice Gavin almost 9 years
    What is the correct way to reference a colour in an <item>?