Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 863
RonnyKnoxville Avatar asked Nov 16 '14 14:11

RonnyKnoxville


1 Answers

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>
like image 101
Salem Avatar answered Oct 04 '22 15:10

Salem