My Android app is crashing because of the following error:
Fatal Exception: android.view.InflateException Binary XML file line
#100 in com.example.myapp:layout/floating_view: Binary XML file line #100 in com.example.myapp:layout/floating_view: Error inflating class <unknown>
The related section of the xml file floating_view.xml, where line #100 is the textColor:
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textColor="?attr/textColor" />
Here it's obviously about the attribute and styles. However, my attribute and styles are defined correctly. As you can see below in my styles.xml, the attribute textColor has a corresponding value in each theme.
<resources xmlns:tools="http://schemas.android.com/tools">
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="textColor">#20282D</item>
</style>
<style name="AppThemeLight" parent="AppTheme">
<item name="textColor">#20282D</item>
</style>
<style name="AppThemeDark" parent="AppTheme">
<item name="textColor">#FFFFFF</item>
</style>
</resources>
However as I dig further in the error log, it has traces of some other theme names:
Caused by java.lang.UnsupportedOperationException Failed to resolve
attribute at index 4: TypedValue{t=0x2/d=0x7f040289 a=-1},
theme={InheritanceMap=[id=0x103013fandroid:style/Theme.DeviceDefault.Light.DarkActionBar,
id=0x1030238android:style/Theme.Material.Light.DarkActionBar,
id=0x1030237android:style/Theme.Material.Light,
id=0x103000candroid:style/Theme.Light,
id=0x1030005android:style/Theme],
Themes=[android:style/Theme.DeviceDefault.Light.DarkActionBar,
forced]}
As you can see above, the log from the affected device mentions some themes like Theme.DeviceDefault.Light.DarkActionBar, android:style/Theme.Material.Light etc.. I initially thought it's because my textColor is not defined in those themes. But I have added only 3 themes, and how can I add my custom attribute inside Android's own themes? Do you think the error is because of that, or what else could this error be about?
Also, retrieving the colors in Java code by providing a default value could be an option. However I have so many colors used in my layouts (only in XML) and moving them all to Java is not really ideal in this case.
Any help is appreciated, thanks!
Related
I'm getting a ThemeEnforcement error with the following. warnings.
Caused by: android.view.InflateException: Binary XML file line #9 in com.yourproject:layout/app_bar_main: Error inflating class com.google.android.material.appbar.AppBarLayout
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.AppCompat (or a descendant).
Typical solutions suggest that i'm not applying Theme.AppCompat to my app theme. except i Am.
<style name="Theme.TestApp" parent="Theme.AppCompat">
</style>
Also, other solutions suggest this is caused by not including the appropriate material libraries, which are included and i have previously been able to successfully compile and run the project.
The project compiles with no errors but in some instances the app will crash out immediatly but in other instances (where i have disabled some components) the crash will happen when layout out other views.
In the latter crashes, the error may have issues inflating a standard Button or TextField.
TLDR not setting custom attr values which are used in layouts causes this error.
Part of the problem was that the error messaging was specifically stating that the problem was i wasn't applying the Theme.AppCompat, but in fact I was.
What I wasn't aware of however was that a nav_header_main.xml file was applying the style AppMenuTitle to a Textfield.
This Style was consuming a custom attribute called my.custom.attr. The issue was caused by the fact that this attr's value had not be set in the theme. So it was essentially null.
<style name="AppMenuTitle" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:textColor">?my.custom.attr</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">18sp</item>
</style>
Now because these attr values were being used in a number of places, I also got errors for inflating your standard Button. And this got really confusing because the buttons were just straight up buttons, with no styling applied directly.
In stead, buttons where obtaining there styles from themes applied to parent containers, whos styles had the buttonStyle properties set.
These buttons were using custom drawables. Which guess what, were also using custom attribute values, which guess what....where also not set in the theme.
Ensuring these custom attrs had values, resolved the error.
<style name="Theme.TestApp" parent="Theme.AppCompat">
<item name="my.custom.attr.used.in.textstyle">#color/black</item>
<item name="my.custom.attr.used.in.drawable">#color/black</item>
</style>
To implement theming, I defined my custom style attributes to avoid overriding system styles
themes.xml
<declare-styleable name="MyThemeBase">
<attr name="myTextColorHighlight" format="reference|color"/>
</declare-styleable>
<style name="MyThemeBase" parent="#style/Theme.AppCompat.Light">
...
</style>
<style name="MY.Theme.AppCompat.Light.Orange" parent="SCThemeBase">
<item name="myTextColorHighlight">#color/textcolor_highlight_orange</item>
</style>
<style name="MY.Theme.AppCompat.Light.Blue" parent="SCThemeBase">
<item name="myTextColorHighlight">#color/textcolor_highlight_blue</item>
</style>
I also defined custom TextAppearance
styles.xml
<style name="My.TextAppearance.Medium" parent="#android:style/TextAppearance.Medium">
<!-- this line caused crash -->
<item name="android:textColor">?attr/myTextColorHighlight</item>
<!-- this worked -->
<!--<item name="android:textColor">#color/textcolor_highlight_orange</item>-->
</style>
In the layout xmls, some TextViews referenced above MY.TextAppearance.Medium style, everything looked so far so good. But I got crash while layouting one TextView which was in a ListView, the stacktrace was
android.view.InflateException: Binary XML file line #9: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:619)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:666)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:691)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.xxx.yyy.MyListAdatper.getView(MyListAdatper.java:46)
The most weird was other TextViews in same style were good
I've no idea why I ran into this. With some debugging I just found that it worked only if I used direct color instead of attr reference (please check out comment in above styles.xml) in definition of My.TextAppearance.Medium.
Anyone can help? Thanks.
I think the problem is with your approach. What you have defined is a styleable, that is, a custom attribute that you would like to be able to assign different values to.
Instead, what you want is an attribute reference, which is a constant id that can be assigned different values depending on the theme.
For that, instead of:
<declare-styleable name="MyThemeBase">
<attr name="myTextColorHighlight" format="reference|color"/>
</declare-styleable>
Just put:
<attr name="myTextColorHighlight" format="reference|color"/>
That is, you just need to remove the "declare-styleable" wrapper. That should make it work without problems.
I have a problem related to this statement with ActionBarSherlock:
android:background="?activatedBackgroundIndicator"
The problem is, If I use this value in a layout like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?activatedBackgroundIndicator">
.....
The adapter that contains this XML crash if I use the Theme:
<style name="AppTheme" parent="#style/Theme.Sherlock.Light.DarkActionBar">
But it works if I change the theme to:
<style name="AppTheme" parent="#style/Theme.Sherlock.Light">
Is this a bug of ActionBarSherlock?? Or I am doing something wrong? Repeat it crashes If I
use the Light.DarkActionBar theme but works if I use the Light theme.
I test to change the:
android:background="?activatedBackgroundIndicator"
for this:
android:background="?android:attr/activatedBackgroundIndicator"
Which is the original attribute and works.
Here is the stacktrace:
FATAL EXCEPTION: main
E/AndroidRuntime( 1574): android.view.InflateException: Binary XML file line #2: Error inflating class...
....
E/AndroidRuntime( 1574): Caused by: android.content.res.Resources$NotFoundException: Resource is not a Drawable (color or path): TypedValue{t=0x2/d=0x7f010047 a=-1}
E/AndroidRuntime( 1574): at android.content.res.Resources.loadDrawable(Resource
Thanks in advance.
I create an issue in the ABS repo and SimonVT said that:
ABS does not define attributes in the DarkActionBar theme on api14+ that it doesn't use itself. The library isn't meant to help you theme your app.
unless ABS uses the attribute we don't set them.
So What I did was to create a layout-v11 folder which that specific layout with:
android:background="?android:attr/activatedBackgroundIndicator"
And that works... Kind of not really what I want, create a new folder just for one file, but it works.
I've been at this for days now, and I am at the point of giving up, so any help is much appreciated!
I've been trying to implement the simonVT numberpicker in my android app. Completely new to android, so including the library, referencing this library and getting everything to compile has been a few days mission in itself. Now I finally have everything compiling I get the following error at runtime:
04-06 10:58:37.126: E/AndroidRuntime(14324): java.lang.RuntimeException:
Unable to start activity ComponentInfo{com.example.goalminder/com.example.goalminder.AddGoal}:
android.view.InflateException: Binary XML file line #81:
Error inflating class net.simonvt.numberpicker.NumberPicker
Here is the opening of my layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/net.simonvt.numberpicker"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
NB - The 'xmlns:app' part above has a yellow warning marker - it's not being used. I included this per another stackoverflow answer re. a similar problem. Have left in to discourage this suggestion.
Here is the xml for the numberpicker:
<net.simonvt.numberpicker.NumberPicker
android:id="#+id/dayPicker"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginRight="10dp"
android:layout_weight="1"/>
I have included the theme as instructed by Simon in my theme file. I wasn't really sure what name to give it, so I called it 'NumberPicker':
<resources>
<!-- Copy one of these attributes to your own theme (choose either dark or light).
<item name="numberPickerStyle">#style/NPWidget.Holo.NumberPicker</item>
<item name="numberPickerStyle">#style/NPWidget.Holo.Light.NumberPicker</item>
-->
<style name="NumberPicker" parent="android:Theme">
<item name="numberPickerStyle">#style/NPWidget.Holo.NumberPicker</item>
</style>
<style name="NumberPicker" parent="android:Theme.Light">
<item name="numberPickerStyle">#style/NPWidget.Holo.Light.NumberPicker</item>
</style>
</resources>
I have also added the following to my android manifest as a child of application:
<activity
android:name="net.simonvt.numberpicker.Numberpicker" />
<activity
android:name="net.simonvt.numberpicker.Scroller" />
I've been all over stackoverflow, so what we have above is a scatter gun approach of everything I have seen recommended so. As stated before, I'm floundering with this and am close to implementing a standard ugly list.
NB - I got all this working with the native android implementation of Numberpicker. I want to use Simon VT's backport version however as I will be looking to support API < 11, which includes Gingerbread which I believe has a 39.7% distribution. Please let me know if you think I don't need to support this far back.
you need add theme for the activity on AndroidManifest.xml:
Example:
<activity android:name="yourActivity" android:theme="#style/SampleTheme.Light"/>
If you don't want to create a theme for your own project, you may do the following to the source code of numberpicker to set it to use the default theme NPWidget_Holo_numberPicker.
Replace the constructor with the following
public NumberPicker(Context context, AttributeSet attrs) {
this(context, attrs, R.style.NPWidget_Holo_NumberPicker);
}
then change the assignment of TypedArray attributesArray to the following:
TypedArray attributesArray = context.obtainStyledAttributes(
attrs, R.styleable.NumberPicker, 0, defStyle);
See Simon's usage notes:
Requires adding a single attribute to your theme. Check the sample app for how this is done.
values/theme.xml:
<style name="SampleTheme" parent="android:Theme">
<item name="numberPickerStyle">#style/NPWidget.Holo.NumberPicker</item>
</style>
values-v11/themes.xml:
<style name="SampleTheme" parent="android:Theme.Holo">
<item name="numberPickerStyle">#style/NPWidget.Holo.NumberPicker</item>
</style>
Try replacing net.simonvt.numberpicker.NumberPicker with com.your.package.NumberPicker.
I was having practically the same problem, I was getting the error
11-18 21:13:18.627: W/ResourceType(13799): No package identifier when getting value for resource number 0x00000000
I finally realised that I had to add the style item into my own style definitions (as Paul Lammertsma shows above) as I was just copy/pasting SimonVT's styles, which of course my application wasn't using:
<style parent="#android:style/Theme.Holo.NoActionBar.Fullscreen" name="NoActionBar">
<item name="numberPickerStyle">#style/NPWidget.Holo.NumberPicker</item>
</style>
Then, after it still not working, I found I'd completely missed a themes.xml file (I have three for different API levels).
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.
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!