63

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.

Community
  • 1
  • 1
Paul Reznik
  • 965
  • 1
  • 6
  • 18

12 Answers12

43

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>
Kevin Grant
  • 2,311
  • 23
  • 17
  • 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. – Kevin Grant Jun 02 '15 at 21:45
  • 10
    Dude... you are the best.. 3 days of banging my head.. you solved the problem. – Mitech Sep 10 '15 at 15:22
39

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

MinnuKaAnae
  • 1,646
  • 3
  • 23
  • 35
orium
  • 3,743
  • 2
  • 24
  • 27
12

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.

Gnod
  • 147
  • 4
9

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

Jainam Jhaveri
  • 1,479
  • 1
  • 18
  • 31
6

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>
dupengtao
  • 109
  • 3
  • 2
    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. – Zoccadoum Aug 30 '16 at 22:38
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'
Arun kumar
  • 1,894
  • 3
  • 22
  • 28
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.

Jian Guo
  • 708
  • 1
  • 9
  • 19
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.

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
Tincho825
  • 829
  • 1
  • 13
  • 15
1

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)

dazza5000
  • 7,075
  • 9
  • 44
  • 89
0

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.

AL.
  • 36,815
  • 10
  • 142
  • 281
0

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" />
avalancha
  • 1,457
  • 1
  • 22
  • 41
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.

navid
  • 1,022
  • 9
  • 20