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

AndroidXmlExceptionAndroid StudioGradle

Android Problem Overview


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.

Android Solutions


Solution 1 - Android

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>

Solution 2 - Android

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

Solution 3 - Android

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: enter image description here 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.

Solution 4 - Android

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>

Solution 5 - Android

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

Solution 6 - Android

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'

Solution 7 - Android

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.

Solution 8 - Android

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.

Solution 9 - Android

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.

Solution 10 - Android

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" />

Solution 11 - Android

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.

Solution 12 - Android

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)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPaul ReznikView Question on Stackoverflow
Solution 1 - AndroidKevin GrantView Answer on Stackoverflow
Solution 2 - AndroidoriumView Answer on Stackoverflow
Solution 3 - AndroidGnodView Answer on Stackoverflow
Solution 4 - AndroiddupengtaoView Answer on Stackoverflow
Solution 5 - AndroidJainam JhaveriView Answer on Stackoverflow
Solution 6 - AndroidArun kumarView Answer on Stackoverflow
Solution 7 - AndroidJian GuoView Answer on Stackoverflow
Solution 8 - AndroidTincho825View Answer on Stackoverflow
Solution 9 - AndroidAL.View Answer on Stackoverflow
Solution 10 - AndroidavalanchaView Answer on Stackoverflow
Solution 11 - AndroidnavidView Answer on Stackoverflow
Solution 12 - Androiddazza5000View Answer on Stackoverflow