69

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.

13 Answers 13

44

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Unfortunately my app does not have the colorAccent attribute, because I'm not using AppCompat. The solution I posted should work for developers who have a similar setup as I have described.
Dude... you are the best.. 3 days of banging my head.. you solved the problem.
41

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

3 Comments

This is the answer.
Can someone explain why?
This was also the answer for me.For clarification, I changed PopupMenu fileMenu = new PopupMenu(getApplicationContext(), fileButton) to PopupMenu fileMenu = new PopupMenu(MainActivity.this, fileButton) and all was working.
13

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.

Comments

10

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

2 Comments

The ?... is my problem
adding the theme work's for me, tks
7

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>

1 Comment

This actually did solve the problem in my case. I had created a custom TextView and was using that custom view in a layout. Worked fine until I changed the application theme in the app manifest and started seeing this error. Changing it back to Theme.AppCompat.Light solved the problem. I presume the alternate theme I had selected didn't have some of the theme elements I was using in my styles.
4

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'

Comments

4

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.

Comments

2

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)

Comments

1

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.

Comments

1

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.

Comments

1

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

Comments

1

If somebody gets similar error "Caused by: java.lang.UnsupportedOperationException: Failed to resolve attribute at index 3: TypedValue" do one of this.

  1. Add <item name="android:colorAccent"> to your theme:

    <item name="colorAccent">@color/green</item>
    <item name="android:colorAccent">?colorAccent</item>
    
  2. Or add parent classes to all required styles. For instance, TextAppearance.AppCompat, Theme.AppCompat or android:Widget.TextView:

    <style name="TextAppearance" parent="TextAppearance.AppCompat">
    
    // or <style name="TextAppearance.H1" parent="TextAppearance.AppCompat">
    

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.