Android XML: RuntimeException: Failed to resolve attribute at index 6 - android

Hello dear stackoverflower,
In my project, i am using the new "android design library".
The problem is, that there is a runtime exception which says(Im trying to create a FloatingButton):
java.lang.RuntimeException: Failed to resolve attribute at index 6
at android.content.res.TypedArray.getColorStateList(TypedArray.java:426)
at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:91)
at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:79)
at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:75)
I was able to figure out, which the attribute cannot be resolved :
<style name="Widget.Design.FloatingActionButton" parent="android:Widget">
<item name="android:background">#drawable/fab_background</item>
<item name="backgroundTint">?attr/colorAccent</item> **!! this one is missing !!**
<item name="fabSize">normal</item>
<item name="elevation">#dimen/fab_elevation</item>
<item name="pressedTranslationZ">#dimen/fab_translation_z_pressed</item>
<item name="rippleColor">?attr/colorControlHighlight</item>
<item name="borderWidth">#dimen/fab_border_width</item>
</style>
This is located in res/values/styles/styles.xml in the android-design-library
i have read in this post that the API lvl should bis 21+. But as the design library supports API 7+, this should not be a problem actually.
It is also worth to mention that i have not included the design library as a gradle-dependency like this:
compile 'com.android.support:design:22.2.0'
I am adding the library manually to the project because the Jenkins server has no Internet access.
I have updated the support-v4 library to 21.2.0
also the appcompat support-v7 is included and updated.
Here is the android-design-library gradle file:
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
minSdkVersion 17
targetSdkVersion 21
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
It would be great if someone can help me.

Ran into this problem myself. It's because my app isn't using AppCompat yet, still just the regular support FragmentActivity. This means that FloatingActionButton was looking for two theme attributes and it couldn't find them.
Specifically adding in these missing attributes made it work for me without the need to start using AppCompat.
<LinearLayout
...
xmlns:app="http://schemas.android.com/apk/res-auto"
.../>
<android.support.design.widget.FloatingActionButton
...
app:backgroundTint="#color/{some color statelist}"
app:rippleColor="#color/{some color statelist}"
... />
</LinearLayout>

Had the same issue because have used getApplicationContext() instead of Activity to retrieve LayoutInflater and inflate item view for list adapter.

You can solved this problem by using Theme.AppCompat.Light as your activity's parent theme.
add:
The reason is that one of the default style using inner in FloatingActionButton is declare like this:
the backgroundTint is refer to another attribute colorAccent which should be measure declared in our theme, otherwise, the exception might be throw.
But colorAccent is declared in AppCompat Theme and did not declared in the sdk default Theme.To measure that we can using the design lib correctly in running, one of the easy way is to measure the using of AppCompat Theme like Theme.AppCompat.Light.

I was using a custom attribute in attrs.xml and a customview. I ran into this problem as I didn't specify theme: attribute in the custom view. Quick look of how the files look
attrs.xml
<resources>
<attr name="textColorPrimary" format="reference" />
...
</resources>
customview.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tvText"
style="#style/TitleBlackThin"
android:theme="#style/AppTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:padding="16dp"
android:text="#string/text" />
In my styles.xml, I extended my style to use AppTheme
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="textColorPrimary">#color/black</item>
</style>
...
<style name="TitleBlackThin">
<item name="android:textSize">20sp</item>
<item name="android:fontFamily">sans-serif-light</item>
<item name="android:textStyle">normal</item>
<item name="android:textColor">?textColorPrimary</item>
</style>
...
</resources>
The culprit here was custom attribute ?textColorPrimary. As I didn't define AppTheme in the customview, it wasn't able to determine how to load textColorPrimary. By android:theme="#style/AppTheme", this got fixed))

Make sure your theme .
My theme :
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#2196F3</item>
<item name="colorPrimaryDark">#1565C0</item>
<item name="colorAccent">#E91E63</item>
</style>

This is because of more than one style.xml files.
Add below line in your app build.gradle file:
compile 'com.android.support:design:22.2.0'

I came across this problem as I create my custom view with a custom attribute, but using applicationContext. I think that the application context misses my attribute's information. Changing to the activity's context fixed my problem here.

In my case, I had an error at setContentView(R.layout.my_layout).
In my_layout, I was using app:errorEnabled="true" in a TextInputLayout which caused the error. Removed that line, and it worked.

I was getting this exception while running tests against a fragment that had a custom view. To fix it, I needed to set a theme when launching the fragment.
launchFragmentInContainer<FulfillmentFragment>(
fragmentArgs = bundleOf(
"foo" to "7",
"bar" to "7"
),
themeResId = R.style.Theme_FooBar
)
https://developer.android.com/reference/kotlin/androidx/fragment/app/testing/package-summary#launchFragmentInContainer(android.os.Bundle,kotlin.Int,androidx.lifecycle.Lifecycle.State,androidx.fragment.app.FragmentFactory)

For my case, this issue started showing up after migrating to AndroidX. The scenario was I implemented custom views that extends AppCompatEditText and set custom styles as well. I ran into this issue while running the unit tests.
The error logs show this as the root cause:
Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 13: TypedValue{t=0x2/d=0x7f0300f9 a=2}
at android.content.res.TypedArray.getDrawableForDensity(TypedArray.java:946)
at android.content.res.TypedArray.getDrawable(TypedArray.java:930)
In the styles I was using Base.Widget.AppCompat.EditText:
<style name="MyEditText" parent="Base.Widget.AppCompat.EditText">
<item name="android:fontFamily">...</item>
<item name="android:textSize">...</item>
</style>
Changing the parent to Android's base android:Widget.EditText removed the error and didn't change any behavior/UI for me.
It's strange since digging through the inheritance structure of Base.Widget.AppCompat.EditText, the root parent is also android:Widget.EditText.

In case anybody comes across this while setting up Android TV / working alongside the AndroidTV sample code, then the solution is to add the leanback theme to your activities in the manifest
<activity
android:name=".PlaybackActivity"
android:theme="#style/Theme.Leanback" />

For some people the problem may be lying under AppCompat/Material theming. To use some material widgets you have to migrate your app theme to it.

Related

How can I call the style of a new Library as parent in style.xml?

I am building a new library for Android and a demo project that I am using to test the library.
The library that I made has a style called Light and in the demo I am applying this style in Java using setTheme(R.style.Light) but I would like to replace it calling the theme directly in style.xml. What I have right now in the demo/res/values/style.xml is
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
and I would like to replace it with something similar to:
<style name="AppTheme" parent="myLibrary.Light">
I have tried with parent="#style/Light" but it's not working.
Does anyone know how I can do it?
To inherit styles from a library or your own project, declare the parent style name without the #android:style/ part. For example, the following example inherits text appearance styles from the support library:
<style name="GreenText" parent="TextAppearance.AppCompat">
<item name="android:textColor">#00FF00</item>
</style>
For more details
I found the solution,
My problem was that in my library I had different styles depending on the version of Android. For each version of Android my main theme was referred to different parents by mistake. I just changed the parents in my library making sure that they are referring to the same one and it fixed my problem. Now in my app I can refer to the theme created in my library without crashing the application.

How to list <item name="app:fabSize">[value]</item> in styles.xml?

I tried to include the app:fabSize in styles.xml but android studio threw an error
Is there any way to include the app:fabSize and other app: attributes in styles.xml?
Here is the code for the style tag
When adding Compat attributes in layout.xml one should use previously defined namespace (it doesn't have to be app actually, but should be defined in xml via xmlns: - for example having hello as a namespace is also possible. Anyway app is some kind of a standard).
<android.support.design.widget.FloatingActionButton
xmlns:hello="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
hello:fabSize="mini"/>
But in styles.xml to use Compat attributes one shouldn't put anything, but attribute name:
<style name="StyleName">
<item name="fabSize">mini</item>
</style>

The following classes could not be found: android.support.v7.internal.app.WindowDecorActionBar

What causes the following error in the layout preview in Android Studio?
Rendering Problems The following classes could not be found:
- android.support.v7.internal.app.WindowDecorActionBar (Fix Build Path, Create Class)
Tip: Try to build the project.
The Actionbar has been deprecated and replaced by Toolbar. That being said, you can do the following if you want to continue using Actionbar for now:
Open styles.xml in the values folder inside the res folder.
Add the word Base to the beginning of the theme name so that it reads "Base.Theme.AppCompat.Light.DarkActionBar"
I had the same issue today and this solution worked for me. FYI I am in Android Studio though, but hopefully,the solution is similar for Eclipse.
FYI here is a decent blog post on replacing the Actionbar with the Toolbar for when you are ready to do so: https://blog.xamarin.com/android-tips-hello-toolbar-goodbye-action-bar/
I think you must be depending on "com.android.support:appcompat-v7:23.1.1" in your module settings.
ActionBar has been deprecated.
Just change your dependencies from 'com.android.support:appcompat-v7:23.1.1' to 'com.android.support:appcompat-v7:23.0.1' in "build.gradle".
You can also change your style parent to "Theme.AppCompat.Light.NoActionBar".
Try to use the Toolbar instead of ActionBar.
This one works for me
Changing AppTheme parent in res/values/styles.xml resolved this problem. Replace
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
with
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
Or you can change API level 21 from list.
goto: res-->values-->styles(V21)-->
Code
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowDrawsSystemBarBackgrounds">true</item>
<item name="android:statusBarColor">#android:color/transparent</item>
</style>
select your theme as Apptheme.NoActionBar in preview. Because ActionBar is depricated, welcome back to ToolBar. NO need to change your dependencies in build.gradle(Module:app).
from com.android.support:appcompat-v7:23.1.1 to com.android.support:appcompat-v7:23.0.1.
Hope this will help you!!!
I had a similar issue, and as many have said since ActionBar has been deprecated the trick was to specify
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
NOTE: that the parent ends in .NoActionBar.
Since ActionBar has been deprecated Android has gone into using ToolBar which you can read about here on the developer.android.com
I tried specifying
<style name="AppTheme.NoActionBar">
As a another post stated and that threw errors. Though others may not have that issue.
I have the same problem in Android Studio 1.5 (even with AppCompatActivity), and my attempt to solve the problem was to update my Android Studio to 2.0. It solves the problem in my case. You can found the link download in: http://tools.android.com/download/studio/canary/latest
thanks #joshgoldeneagle, worked in AS v1.4 for me. also effective in AS is to open "build.gradle (Module.app)" and change version from v7.23.1.1 to v7.23.0.1 -hth
Quick fix
Select a theme with no AtcionBar in UI Preview Tool
You will need to change the api rendering level to 17, there may be rendering problem with higher level apis, might be higher level apis suppose to have a default theme, and hope you are not specified none.
For AS v1.4 adding "Base" before Theme.AppCompat.Light.DarkActionBar in the styles.xml folder solved the problem
Additionally, updating to AS v2.1 also solved the problem. Which is better likely depends on collaborators and their AS version.
In res/values/styles.xml, you will find your AppTheme as below :-
Change it to :-
by adding "Base" the rendering issue will get resolved.
Changing the theme in the manifest solved my problem.
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

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

Missing styles. Is the correct theme chosen for this layout?

Missing styles. Is the correct theme chosen for this layout? Use the Theme combo box above the layout to choose a different layout, or fix the theme style references. Failed to find style mapViewStyle in current theme.
I tried every solutions available to solve this problem but nothing seems to work. I have included library in the manifest file. I even created style is styles.xml, I have chosen Google Apis build target as well.
Can somebody please give me a solution?
here is my xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
style="#style/AppTheme"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.google.android.maps.MapView
android:id="#+id/themap"
style="#style/mapViewStyle"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="here i have my key"
android:clickable="true"
android:enabled="true" />
</RelativeLayout>
Here is my manifest snippet:
<uses-library android:name="com.google.android.maps" />
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Second" />
<activity android:name=".Third" android:theme="#android:style/Theme.Black"/>
</application>
here is my style.xml file
<resources>
<style name="mapViewStyle" parent="#android:style/Theme.Black">
</style>
</resources>
For Android Studio (or IntelliJ IDEA),
If everything looks OK in your project and you're still receiving the error in your layouts, try to 'Invalidate caches & restart'.
Enjoy a coffee while Android Studio is recreating caches & indexes.
I had the same problem and found it was the Theme dropdown at the top of the graphical layout editor. I changed from Holo to Theme and the layout displayed and error disappeared.
What I usually do is the following: a Gradle Clean, Rebuild and Sync all my Gradle files. After that I restart Android Studio, and I go to:
Select Theme -> Project Themes -> AppTheme
This is because you select not a theme of your project. Try to do next:
If we are creating too many projects using Android Studio sometime Rendering issue comes. Even creating a blank activity we receive such rendering error.
Please shut down and restart Android studio. In case the issue persists you can
Hope this helps. It works similar fashion as rule of thumb, in case all looks good and still error persists a restart of application usually resolves the issue.
In my case the problem occurred while the default setting for Android Version in the Designer was set to 'Preview N'. Changed Android Version to '23' and the error notification went away.
Edit And don't forget to uncheck 'Automatically Pick Best'.
I had the same problem using Android Studio 1.5.1.
This was solved by using the Android SDK Manager and updating Android Support Library, as well as Local Maven repository for Support Libraries.
After updating the SDK and restarting Android Studio, the problem was rectified.
Hope this helps anyone who has the same problem after trying other suggestions.
I solved it by changing "classpath" in "dependencies" in build.gradles.
Just change:
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0-alpha2'
or something like that to:
dependencies {
classpath 'com.android.tools.build:gradle:2.2.3'
I got this error after overriding action bar style like this. Also i lost action bar in preview.
<style name="general_app_theme" parent="#style/Theme.AppCompat">
<!-- for API level 11 -->
<item name="android:actionBarStyle">#style/action_bar_style</item>
<!-- Support library compatibility -->
<item name="actionBarStyle">#style/action_bar_style</item>
</style>
<style name="action_bar_style" parent="#style/Widget.AppCompat.ActionBar">
<!-- for API level 11 -->
<item name="android:height">#dimen/action_bar_height</item>
<item name="android:background">#color/action_bar_color</item>
<item name="android:displayOptions">showTitle</item>
<!-- Support library compatibility -->
<item name="displayOptions">showTitle</item>
</style>
So, problem is overriding theme by my custom. this is the reason, why I've changed AppCompat (from "Project Themes") to general_app_theme (from "Manifest Themes"). Now I have no this error and action bar in preview come back.
Try switching the theme in the design view to one of the options in "Manifest Themes". I'm guessing this is Because you are inheriting the theme from the manifest to support multiple api levels. It worked for me anyway.
With reference to the answer provided by #Benno:
Instead of changing to a specific theme, you can simply choose 'DeviceDefault' from the theme menu. This should choose the theme most compatible with the emulated device.
If you still have the problem after trying all the solutions above, please try modify the API Level as shown below. Incorrect API Level may also cause the problem.
It can be fixed by changing your theme in styles.xml from
Theme.AppCompat.Light.DarkActionBar to Base.Theme.AppCompat.Light.DarkActionBar
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
to
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
and clean the project. It will surely help.
Most of the errors in XML happen due to the names you have given to the resources.
Images stored in the drawable folder need to be named in a proper manner.
just check these things:
Are there any duplicate files? if so remove the duplicates. (For example "image.jpg" and "image.png" are not allowed in the same time. It will not show any errors but sometimes it will show that R.java cannot be resolved)
Do the filenames contains any uppercase letters? If so right click on that file->refactor->rename
and rename it with all lowercase, no hyphens, and only a-z and 0-9 are allowed.
Just make sure of the above cases.
I got the same problem with my customized theme that used Holo.Light as its parent. In grayed text Android Studio indicated that some attributes were missing. When I added these missing attributes as follows, the rendering problems went away -
<item name="android:textEditSuggestionItemLayout"></item>
<item name="android:textEditSuggestionContainerLayout"></item>
<item name="android:textEditSuggestionHighlightStyle"></item>
Even though they introduced errors in my style's theme, they caused no problems in rendering the activity designs or building my app.
You can simply remove this error through change "App theme" and select any theme such as light. This error will be removed.
I meet the same problem.Select theme to another one in preview can solve problem temporary.Like the image
Finally,I found out that there are AppTheme and AppBaseTheme in my styles.xml.
And AppTheme has parent AppBaseTheme.
The AppBaseTheme has parent of one system style.
And every layout file use the theme AppTheme.
Just change AppTheme's parent to AppBaseTheme's parent.
It solve the problem permanent.
Just need click button 'AppTheme', and choose 'Manifest Themes'. It works in most cases.
For me, it was occurring on menu.xml file as i was using android:Theme.Light as my theme, So what i did was -
Added new Folder in res directory named values-v21.
Added android:Theme.Material.Light as AppTheme in styles.xml.
This is very late but i like to share my experience this same issue.
i face the same issue in Android studio i tried to some other solution that i found in internet but nothing works for me unless i REBUILD THE PROJECT and it solve my issue.
Hope this will works for you too.
Happy coding.
Change your App theme to AppCombat..

Categories

Resources