2012-08-11 77 views
4

我想设置一个基于当前主题属性的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 = 0×2/d = 0x7f010068 A = -1}

我敢肯定,我做错了什么,因为它与Android版属性,但我无法在Google搜索过程中找到它。

我希望你能帮助我!

非常感谢,

Sébastien。

+0

您是否在清单中添加主题? – Luksprog 2012-08-12 07:53:57

+0

是的:)其实,我有一些其他属性,我从代码查询,它运作良好。我只需要有这个工作从XML ... – 2012-08-12 11:30:54

+0

我已经运行你的代码,它的工作完美无瑕,所以你的代码中必须有别的东西。您是否尝试重新启动Eclipse并清理该项目? – Luksprog 2012-08-12 12:05:54

回答

3

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

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

也许它会帮助别人谁会犯同样的错误!

+0

如果你仍然卡住了?我建议看看这里:http://stackoverflow.com/questions/2118251/theme-style-is-not-applied-when-inflater-used-with-applicationcontext – TouchBoarder 2014-09-02 08:43:55

相关问题