Thursday, August 25, 2011

Error retrieving parent for item

Some days ago I updated the android platform-tools to r6. Everything went fine and it really called my attention :)

Yesterday I tried to open a one month old honeycomb application and I got errors in my styles.xml file.

Eclipse said:

error: Error retrieving parent for item: No resource found that matches the given name 'android:style/Widget.Holo.Light.ActionBarView_TabView'.

After:
  • Trying to build with eclipse
  • Trying to build with mvn from the cmd line
  • Removing my .m2 repo
  • Replacing my android jar with another
  • Comparing the mvn -X output with someone's who had it working
I discovered issue 18659: aapt cannot resolve parent reference to @android:style/WindowTitle

Formal explanation:

What is happening is that some styles, like WindowTitle are not public (you won't find them in android.R.style). You should not be extending non public resources. aapt used to let you do that but it was a bug which was fixed in platform-tools r6.

Ok, great I can't extend non public resources, but my style code was copy and pasted from @crafty's blog post: Customizing the Action Bar!

After testing his code I noticed that it also didn't work. Note: There is an issue created for this.

Just for the record, here's how to fix it.
What we need to do is avoid extending the non public resources and copying all the items in our own style.

The two styles that generate the error are:

<style name="MyActionBarTabStyle" parent="android:style/Widget.Holo.Light.ActionBarView_TabView">
<item name="android:background">@drawable/actionbar_tab_bg</item>
<item name="android:paddingLeft">32dp</item>
<item name="android:paddingRight">32dp</item>
</style>

<style name="MyDropDownNav" parent="android:style/Widget.Holo.Light.Spinner.DropDown.ActionBar">
<item name="android:background">@drawable/ad_spinner_background_holo_light</item>
<item name="android:popupBackground">@drawable/ad_menu_dropdown_panel_holo_light</item>
<item name="android:dropDownSelector">@drawable/ad_selectable_background</item>
</style>


Let's fix MyActionBarTabStyle.
To fix it, we will need to copy every non modified items to our style.
We need to open styles.xml at $ANDROID_SDK/platforms/android-11/data/res/values

There we see:

<style name="Widget.Holo.Light.ActionBarView_TabView" parent="Widget.Holo.ActionBarView_TabView">
</style>

No items there, let's now search for: Widget.Holo.ActionBarView_TabView.

<style name="Widget.Holo.ActionBarView_TabView" parent="Widget.ActionBarView_TabView">
<item name="android:background">@drawable/tab_indicator_holo</item>
<item name="android:paddingLeft">16dip</item>
<item name="android:paddingRight">16dip</item>
</style>

There we find three items and we still need to continue up with Widget.ActionBarView_TabView:

<style name="Widget.ActionBarView_TabView">
<item name="android:background">@drawable/minitab_lt</item>
<item name="android:paddingLeft">4dip</item>
<item name="android:paddingRight">4dip</item>
</style>


Great. The same three items and we already have it in our own style. We can get rid of the extension. Leaving our style like this:

<style name="MyActionBarTabStyle">
<item name="android:background">@drawable/actionbar_tab_bg</item>
<item name="android:paddingLeft">32dp</item>
<item name="android:paddingRight">32dp</item>
</style>

The fixed MyDropDownNav ends up extending something non public. It should look like this:

<style name="MyDropDownNav" parent="android:style/Widget.Holo.Light.Spinner">
<item name="android:background">@drawable/ad_spinner_background_holo_light</item>
<item name="android:popupBackground">@drawable/ad_menu_dropdown_panel_holo_light</item>
<item name="android:dropDownSelector">@drawable/ad_selectable_background</item>
</style>


I honestly don't understand what makes an style private or not. They only way I found is checking android.R.style class. If someone knows a better way, please let me know!

Conclusion
Android development has this type of tricks. Fortunately the active community report issues and write about workarounds so everyone can avoid some of the pain :)

Happy coding!

2 comments:

  1. Thanx for the post...really appreciate..
    I resolved the problem just by removing parent from styles.xml.
    I was struggleing for this from last few months,I followed endless links but al in vain. They didnt give any thing useful.
    So, here is the simple solution in my case.

    Code with error:

    style name="iWindowTitleBackground" parent="@*android:style/WindowTitleBackground" >
    item name="android:background">@drawable/black_toolbar/item
    /style


    Without error:

    style name="iWindowTitleBackground" >
    item name="android:background">@drawable/black_toolbar/item
    /style

    ReplyDelete
  2. I'm glad I stumbled across your post and saw that I'm not the only one struggling with Android's layout system! Basically, it's a pretty good idea, but sometimes it's implemented really bad. Not just as seen in your post, but it took me a whole day to change the color of the divider between title and message in a Holo theme dialog, simply because it cannot be defined but it's hard-coded.

    Hopefully, they'll improve it soon.

    ReplyDelete