根据主题属性设置视图背景会使应用程序崩溃

Séb*_*ien 7 android android-layout

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

这是我在做什么:

首先,创建背景颜色:

<resources>
    <color name="userlist_background_light">#fff0f0f0</color>
    <color name="userlist_background_dark">#ff040404</color>
</resources>
Run Code Online (Sandbox Code Playgroud)

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

<resources>
    <attr name="userlist_background" format="reference|color" />
</resources>
Run Code Online (Sandbox Code Playgroud)

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

<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>
Run Code Online (Sandbox Code Playgroud)

最后,在 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" />
Run Code Online (Sandbox Code Playgroud)

甚至 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 搜索中找不到什么。

我希望你能帮助我!

非常感谢,

塞巴斯蒂安。

Séb*_*ien 8

好的,我修复了它,这是由于错误!

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

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