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

后端 未结 10 1896
梦如初夏
梦如初夏 2020-12-01 23:44

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 c

相关标签:
10条回答
  • 2020-12-02 00:08

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

    0 讨论(0)
  • 2020-12-02 00:08

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

    0 讨论(0)
  • 2020-12-02 00:10

    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>
    
    0 讨论(0)
  • 2020-12-02 00:10

    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.

    0 讨论(0)
提交回复
热议问题