Java.lang.UnsupportedOperationException : Can’t convert to color: type=0x1 exception
I’ve been following the myfirstapp guide on Android developer training, but I’m running into a problem they do not properly explain how to define colors.
They mention that to create a custom theme, you can declare the text color like this:
Subject .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 don’t mention how to specify @color/actionbar_text
, but common sense (and some Google searches) suggests that a colors.xml file is needed in the value pack:
Color .xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="actionbar_text">#ff355689</color>
</resources>
However, when trying to run the application, an error occurs:
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 gets an error when the color reference is not found. I’m relatively sure my code is correct, can anyone see any errors?
Edit
I
just wanted to point out that I actually use a slightly different syntax for the themes.xml file because the syntax in the tutorial doesn’t compile. The tutorial uses @style/widget.Holo.ActionBar.TabText, which I found to be an Android property, so I needed to use @android: style/widget.Holo.ActionBar.
TabText
instead.
Solution
If I’m not mistaken, the code says Android found the reference when it expected the color value or couldn’t convert the reference to color. Looking at your code, this line stands out
<item name="android:textColor">@style/MyActionBarTitleText</item>
Although you can
reference it in textColor, I’m not sure if you can style it.
So try to quote your colors directly
<item name="android:textColor">@color/actionbar_text</item>