【问题标题】:java.lang.UnsupportedOperationException: Can't convert to color: type=0x1java.lang.UnsupportedOperationException:无法转换为颜色:type=0x1
【发布时间】:2014-11-16 14:45:31
【问题描述】:

我一直在关注关于 Android 开发人员培训的 myfirstapp 指南,但我遇到了一个问题,they do not properly explain how to define colors.

他们提到要创建自定义主题,您可以这样声明文本颜色:

主题.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,但常识(和一些谷歌搜索)表明 values 包中需要一个 colors.xml 文件:

colors.xml

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

但是,当尝试运行应用程序时,它会报错:

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 中删除该行,它会在找不到颜色参考时出错。我比较确定我的代码是正确的,任何人都可以看到任何错误吗?

编辑

我只是想指出,我实际上对themes.xml 文件使用了稍微不同的语法,因为教程中的那些不编译。本教程使用@style/Widget.Holo.ActionBar.TabText,我发现它实际上是android的一个属性,所以我需要使用@android:style/Widget.Holo.ActionBar.TabText来代替。

【问题讨论】:

    标签: java android xml android-styles


    【解决方案1】:

    如果我没记错的话,该代码表示​​ Android 在预期颜色值或未能将引用转换为颜色时找到了引用。查看您的代码,这一行很突出

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

    尽管您可以在 textColor 中有参考,但我不确定您是否可以将其设置为样式。

    所以尝试直接参考你的颜色

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

    【讨论】:

    • 绝对正确。有时你只是无法透过树木看到树林。谢谢!
    • 中引用颜色的正确方法是什么?
    【解决方案2】:

    就我而言,我将 com.github.amlcurran.showcaseview:library:+ 更改为特定版本 即 com.github.amlcurran.showcaseview:library:5.4.3 它工作正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      • 1970-01-01
      相关资源
      最近更新 更多