Android XML: RuntimeException: Failed to resolve a

2019-01-12 05:49发布

问题:

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.

回答1:

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>


回答2:

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



回答3:

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.



回答4:

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>


回答5:

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'


回答6:

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.



回答7:

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