【问题标题】:Setting a View background based on theme attribute crash the app根据主题属性设置视图背景使应用程序崩溃
【发布时间】:2012-08-12 21:48:28
【问题描述】:

我正在尝试根据当前主题属性设置 ListView 背景颜色,但每次显示 ListView 时它都会崩溃。似乎我做错了什么但我看不到什么...

这就是我正在做的事情:

首先,创建背景颜色:

<resources>
    <color name="userlist_background_light">#fff0f0f0</color>
    <color name="userlist_background_dark">#ff040404</color>
</resources>

其次,为我的自定义主题创建属性:

<resources>
    <attr name="userlist_background" format="reference|color" />
</resources>

三、在我的主题中设置这个属性:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <style name="Light" parent="Theme.Sherlock.Light">
        <item name="userlist_background">@color/userlist_background_light</item>
    </style>

    <style name="Dark" parent="Theme.Sherlock">
        <item name="userlist_background">@color/userlist_background_dark</item>
    </style>
</resources>

最后,在 ListView xml 中使用这个属性:

<ListView
        android:id="@+id/user_bar"
        android:layout_width="0dip"
        android:layout_height="0dip"
        android:background="?attr/userlist_background"
        android:cacheColorHint="?userlist_background"
        android:visibility="gone" />

即使是 Eclipse 布局视图也会崩溃。当然,如果我直接在背景属性中使用“@color/”就可以了。如果我使用说“?android:attr/colorBackground”,它甚至可以工作。

消息错误是:

android.view.InflateException:二进制 XML 文件第 8 行:错误 膨胀类 android.view.ListView 原因: android.content.res.Resources$NotFoundException:资源不是 可绘制(颜色或路径):TypedValue{t=0x2/d=0x7f010068 a=-1}

我很确定我做错了什么,因为它适用于 android 属性,但我无法在我的 Google 搜索中找到什么。

希望你能帮助我!

非常感谢,

塞巴斯蒂安。

【问题讨论】:

  • 你是否在清单中添加了主题?
  • 是的 :) 实际上,我从代码中查询了其他一些属性,并且效果很好。我只需要让这个工作从 xml...
  • 我已经运行了你的代码,它完美无瑕,所以你的代码中一定有其他东西。您是否尝试重新启动 Eclipse 并清理项目?
  • 非常感谢。的确,这是一个错误。我有两个 theme.xml 文件,一个用于 Honeycomb+ 用于 Holo 主题,一个用于其他机器人。我只更改了一个文件,并且正在 ICS 上进行测试,它解释了崩溃。对不起,谢谢!

标签: android android-layout


【解决方案1】:

好的,我修好了,是一个错误!

我有两个themes.xml 文件,一个用于 Honeycomb+,一个用于 Gingerbread-。我只将新属性添加到themes.xml 的目标 Gingerbread-,并且正在 ICS 上进行测试。

也许它会帮助其他会犯同样错误的人!

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-21
  • 1970-01-01
  • 2015-05-29
相关资源
最近更新 更多