Crash with android:background="?activatedBackgroundIndicator" - android

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.

Related

Error inflating class <unknown> in XML file

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!

ThemeEnforcement error actually caused by unset attrs

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>

Android: TextView property "textAllCaps" causes XML Inflate error when fontFamily is set to sans-serif-condensed

This is an interesting error, and I've been able to reproduce it consistently. I wonder if anyone else has noticed it; I haven't been able to find any reports on it.
The TextView widget has a property called textAllCaps that is used to force any text displayed in it to be in all caps. This property has been in Android since API level 14 (Ice Cream Sandwich). However, I discovered recently that if you set the Font Family for your app to sans-serif-condensed by putting this in styles.xml:
<item name="android:fontFamily">sans-serif-condensed</item>
Then when any layout is inflated that has a TextView with textAllCaps=true, then it crashes with an InflateException:
12-05 09:18:48.847 16727 16727 E AndroidRuntime: android.view.InflateException: Binary XML file line #49: Binary XML file line #49: Error inflating class TextView
12-05 09:18:48.847 16727 16727 E AndroidRuntime: Caused by: android.view.InflateException: Binary XML file line #49: Error inflating class TextView
12-05 09:18:48.847 16727 16727 E AndroidRuntime: Caused by: java.lang.ArrayIndexOutOfBoundsException: length=57; index=2491
Has anyone encountered this before?
I've seen this, and setting the TextView style property to the app theme style prevents crashes for me.
Example:
<style names="MyAppTheme">
<item name="android:fontFamily">sans-serif</item>
</style>
then:
<TextView
style="#style/MyAppTheme"
...
android:textAllCaps="true" />
Edit for additional info:
After looking at this further my best guess is the bug lies in the TextView code. The original crash is an indexOutOfBoundsException, which in the question shows this:
length=57; index=2491
I think the 57 element list is the style information available to the TextView, and the 2491+ element list is the style information applied to the TextView including the AppTheme information. The style item at position 2491 is likely required for the textAllCaps property to be applied. The issue is likely the TextView isn't made aware of the extra style information unless set explicitly, hence why the above fix circumvents the issue.
This a know bug and can be tracked here - https://issuetracker.google.com/issues/64053344 .
Here is a quick workaround for you. Change you code
From :
<style name="AppCompatTheme" parent="android:...">
<item name="fontFamily">sans-serif-condensed</item>
</style>
To this :
<style name="AppCompatTheme" parent="android:...">
<item name="fontFamily">#string/default_font</item>
</style>
And define default_font in strings.xml file.

Android - Error inflating SimonVT NumberPicker class in my layout xml

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

Setting a View background based on theme attribute crash the app

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!

Categories

Resources