7

I'm trying to set a ListView background color based on the current theme attribute, but it crash every time the ListView is shown.It seems I'm doing something wrong but I can't see what...

Here's what I'm doing:

First, create the background color:

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

Second, create attributes for my custom themes:

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

Third, setting this attribute in my themes:

<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>

And finally, using this attribute in the 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" />

Even the Eclipse layout view crash. Of course, it works fine if I use a "@color/" directly in the background attribute. It even work if I use say, "?android:attr/colorBackground".

The message error is:

android.view.InflateException: Binary XML file line #8: Error inflating class android.view.ListView Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f010068 a=-1}

I'm pretty sure I'm doing something wrong, as it works with android attributes, but I haven't be able to find what during my Google searches.

I hope you'll be able to help me!

Many thanks,

Sébastien.

Sébastien
  • 415
  • 3
  • 11
  • Did you add the theme in the manifest? – user Aug 12 '12 at 07:53
  • Yes :) Actually, I have some others attribute I query from code, and it works well. I just need to have this working from xml... – Sébastien Aug 12 '12 at 11:30
  • I've run you code and it works flawless so there must be something else in your code. Did you try restarting Eclipse and cleaning the project? – user Aug 12 '12 at 12:05
  • Thank you very much. Indeed, it was a mistake. I have two themes.xml files, one for Honeycomb+ for Holo theme, and one for other androids. I changed only one file, and was testing on ICS, it explains the crash. Sorry, and thanks ! – Sébastien Aug 12 '12 at 13:46

1 Answers1

8

Ok, I fixed it, and it was due to a mistake!

I have two themes.xml files, one for Honeycomb+, and one for Gingerbread-. I've only added the new attributes to the themes.xml targeting Gingerbread-, and was testing on ICS.

Maybe it'll help others who'll make the same mistake!

Sébastien
  • 415
  • 3
  • 11
  • if you are still stuck? I suggest looking here: http://stackoverflow.com/questions/2118251/theme-style-is-not-applied-when-inflater-used-with-applicationcontext – TouchBoarder Sep 02 '14 at 08:43