java.lang.UnsupportedOperationException:无法转换为color:type = 0x1 [英] java.lang.UnsupportedOperationException: Can't convert to color: type=0x1
问题描述
我一直在关注Android开发人员培训的myfirstapp指南,但我遇到了一个问题他们没有正确解释如何定义颜色。
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
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>
他们没有提到如何指定 @ color / actionbar_text
,但常识(以及一些谷歌搜索)表示值包中需要colors.xml文件:
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
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
如果我从colors.xml中删除该行,则在找不到颜色引用时会出错。我相对肯定我的代码是正确的,任何人都可以看到任何错误吗?
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?
编辑
我只是想要注意我实际上对themes.xml文件使用略有不同的语法,因为教程不会编译。本教程使用 @ style / Widget.Holo.ActionBar.TabText
,我发现它实际上是android的属性,所以我需要使用 @android :style / Widget.Holo.ActionBar.TabText
而不是。
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.
推荐答案
如果我没弄错的话该代码意味着Android在预期颜色值或未能将引用转换为颜色时找到引用。看看你的代码,这一行脱颖而出
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>
尽管你可以在textColor中有一个引用,但我不确定你可以将它设置为样式。
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>
这篇关于java.lang.UnsupportedOperationException:无法转换为color:type = 0x1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!