Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference a theme attribute in a style?

I've got a following problem. I want to customize appearance of row in a list, but exception is thrown during inflating.

piece of code in style.xml

<style name="AppTheme.Main">     
    <item name="item_shadowColor">#000000</item>        
</style>
<style name="item_label">    
    <item name="android:shadowColor">?attr/item_shadowColor</item>        
</style>

attr.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="item_shadowColor" format="color" />
</resources>

list_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <TextView
        android:id="@id/title"
        style="@style/item_label"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</LinearLayout>

And the exception

FATAL EXCEPTION: main
 android.view.InflateException: Binary XML file line #7: Error inflating class <unknown>
    at android.view.LayoutInflater.createView(LayoutInflater.java:606)
    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    at ru.mts.goodok.adapter.CategoryListAdapter.getView(CategoryListAdapter.java:30)
    at android.widget.AbsListView.obtainView(AbsListView.java:2052)
    at android.widget.ListView.makeAndAddView(ListView.java:1820)
    at android.widget.ListView.fillDown(ListView.java:672)
    at android.widget.ListView.fillFromTop(ListView.java:732)
    at android.widget.ListView.layoutChildren(ListView.java:1673)
    at android.widget.AbsListView.onLayout(AbsListView.java:1882)
    at android.view.View.layout(View.java:11425)
    at android.view.ViewGroup.layout(ViewGroup.java:4232)
    at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
    at android.view.View.layout(View.java:11425)
    at android.view.ViewGroup.layout(ViewGroup.java:4232)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
    at android.view.View.layout(View.java:11425)
    at android.view.ViewGroup.layout(ViewGroup.java:4232)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
    at android.view.View.layout(View.java:11425)
    at android.view.ViewGroup.layout(ViewGroup.java:4232)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
    at android.view.View.layout(View.java:11425)
    at android.view.ViewGroup.layout(ViewGroup.java:4232)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1516)
    at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2505)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.java:4945)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
    at android.view.LayoutInflater.createView(LayoutInflater.java:586)
    ... 39 more
 Caused by: java.lang.NumberFormatException: Invalid int: "?2130771972"
    at java.lang.Integer.invalidInt(Integer.java:138)
    at java.lang.Integer.parse(Integer.java:375)
    at java.lang.Integer.parseInt(Integer.java:366)
    at com.android.internal.util.XmlUtils.convertValueToInt(XmlUtils.java:123)
    at android.content.res.TypedArray.getInt(TypedArray.java:254)
    at android.widget.TextView.<init>(TextView.java:829)
    at android.widget.TextView.<init>(TextView.java:489)
    ... 42 more

What am I doing wrong and how could I solve this problem?

like image 926
Bracadabra Avatar asked Jan 18 '13 12:01

Bracadabra


People also ask

How do you differentiate themes from styles?

A style is a collection of attributes that specify the appearance for a single View . A style can specify attributes such as font color, font size, background color, and much more. A theme is a collection of attributes that's applied to an entire app, activity, or view hierarchy—not just an individual view.

Is style xml and theme xml same?

Yes, the reason people use styles. xml and themes. xml is to differentiate styles that are applied to single views and styles that are applied as themes to entire activities/applications. It just makes it easier to sift through all the different styles.

What is attr in android?

The Attr interface represents an attribute in an Element object. Typically the allowable values for the attribute are defined in a schema associated with the document.


2 Answers

The problem has been solved. I used application context to create adapter, when I changed context to activity the problem disappeared.

like image 62
Bracadabra Avatar answered Nov 06 '22 17:11

Bracadabra


Try to create the themes.xml file and add the below lines in it.

Themes.xml

<style name="Theme">
   <item name="item_shadowColor">@style/item_label</item>
</style>

attrs.xml
Change the the format="reference" in your attrs.xml file.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="item_shadowColor" format="reference" />
</resources>

styles.xml

<style name="AppTheme.Main">     
    <item name="item_shadowColor">#000000</item>        
</style>

<style name="item_label">    
    <item name="android:shadowColor">#F240FF</item>        
</style>

Then after define your style to the TextView as below:

<TextView
    android:id="@id/title"
    style="@style/item_label"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"/>
like image 29
GrIsHu Avatar answered Nov 06 '22 16:11

GrIsHu