【问题标题】:Android XML: RuntimeException: Failed to resolve attribute at index 6Android XML:RuntimeException:无法解析索引 6 处的属性
【发布时间】:2015-08-16 04:37:42
【问题描述】:

亲爱的stackoverflower,你好,

在我的项目中,我正在使用新的“android 设计库”。 问题是,有一个运行时异常说(我正在尝试创建一个 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)

我能够弄清楚,哪个属性无法解析:

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

这位于 android-design-library 的 res/values/styles/styles.xml 中

我在 this 帖子中读到 API lvl 应该是 21+。但由于设计库支持 API 7+,这实际上应该不是问题。

还值得一提的是,我没有像这样将设计库包含为 gradle-dependency:

 compile 'com.android.support:design:22.2.0'

我正在手动将库添加到项目中,因为 Jenkins 服务器无法访问 Internet。 我已将 support-v4 库更新为 21.2.0 还包括并更新了 appcompat support-v7。

这里是 android-design-library 的 gradle 文件:

android {
compileSdkVersion 21
buildToolsVersion "21.1.2"

defaultConfig {
    minSdkVersion 17
    targetSdkVersion 21
}

buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
    }
}

如果有人可以帮助我,那就太好了。

【问题讨论】:

    标签: android xml exception android-studio gradle


    【解决方案1】:

    我自己遇到了这个问题。这是因为我的应用还没有使用 AppCompat,仍然只是常规支持FragmentActivity。这意味着FloatingActionButton 正在寻找两个主题属性,但找不到它们。

    专门添加这些缺失的属性使其对我有用,而无需开始使用 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>
    

    【讨论】:

    • 不幸的是,我的应用没有 colorAccent 属性,因为我没有使用 AppCompat。我发布的解决方案应该适用于与我描述的设置类似的开发人员。
    • 老兄...你是最棒的.. 3 天敲我的脑袋.. 你解决了问题。
    【解决方案2】:

    您可以通过使用Theme.AppCompat.Light 作为活动的父主题来解决此问题。 添加: 原因是在 FloatingActionButton 中使用内部的默认样式之一是这样声明的: backgroundTint 是指另一个属性 colorAccent,它应该在我们的主题中声明,否则可能会抛出异常。 但是colorAccent是在AppCompat Theme中声明的,并没有在sdk默认主题中声明。要衡量我们在运行中是否可以正确使用设计库,一种简单的方法是衡量AppCompat Theme的使用情况,如Theme.AppCompat.Light

    【讨论】:

      【解决方案3】:

      确保您的主题。

      我的主题:

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

      【讨论】:

      • 这实际上确实解决了我的问题。我创建了一个自定义 TextView 并在布局中使用该自定义视图。工作正常,直到我更改应用程序清单中的应用程序主题并开始看到此错误。将它改回 Theme.AppCompat.Light 解决了这个问题。我认为我选择的备用主题没有我在样式中使用的一些主题元素。
      【解决方案4】:

      这是因为 style.xml 文件不止一个。

      在您的应用程序 build.gradle 文件中添加以下行:

      compile 'com.android.support:design:22.2.0'
      

      【讨论】:

        【解决方案5】:

        遇到了同样的问题,因为使用 getApplicationContext() 而不是 Activity 来检索 LayoutInflater 并为列表适配器膨胀项目视图。

        【讨论】:

        • 这就是答案。
        • 谁能解释一下原因?
        • 这也是我的答案。为了澄清,我将PopupMenu fileMenu = new PopupMenu(getApplicationContext(), fileButton)更改为PopupMenu fileMenu = new PopupMenu(MainActivity.this, fileButton),一切正常。
        【解决方案6】:

        就我而言,setContentView(R.layout.my_layout) 出现错误。 在my_layout 中,我在TextInputLayout 中使用了app:errorEnabled="true",这导致了错误。删除了那行,它起作用了。

        【讨论】:

          【解决方案7】:

          我在 attrs.xml 和 customview 中使用了自定义属性。我遇到了这个问题,因为我没有在自定义视图中指定 theme: 属性。快速查看文件的外观

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

          在我的 styles.xml 中,我扩展了我的样式以使用 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>
          

          这里的罪魁祸首是自定义属性 ?textColorPrimary。由于我没有在 customview 中定义 AppTheme,它无法确定如何加载 textColorPrimaryandroid:theme="@style/AppTheme" 已解决))

          【讨论】:

          • ?... 是我的问题
          【解决方案8】:

          就我而言,这个问题是在迁移到 AndroidX 后开始出现的。场景是我实现了扩展AppCompatEditText 的自定义视图并设置了自定义样式。我在运行单元测试时遇到了这个问题。

          错误日志显示这是根本原因:

          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)
          

          在我使用Base.Widget.AppCompat.EditText的样式中:

          <style name="MyEditText" parent="Base.Widget.AppCompat.EditText">
              <item name="android:fontFamily">...</item>
              <item name="android:textSize">...</item>
          </style>
          

          将父级更改为 Android 的基础 android:Widget.EditText 消除了错误,并没有改变我的任何行为/UI。

          奇怪的是,自从挖掘了Base.Widget.AppCompat.EditText的继承结构,root parent也是android:Widget.EditText

          【讨论】:

            【解决方案9】:

            如果有人在设置 Android TV/使用 AndroidTV 示例代码时遇到此问题,则解决方案是将leaback 主题添加到清单中的活动中

            <activity
              android:name=".PlaybackActivity"
              android:theme="@style/Theme.Leanback" />
            

            【讨论】:

              【解决方案10】:

              我在使用自定义属性创建自定义视图时遇到了这个问题,但使用的是applicationContext。我认为应用程序上下文错过了我的属性信息。更改为活动的上下文解决了我的问题。

              【讨论】:

                【解决方案11】:

                对于某些人来说,问题可能在于 AppCompat/Material 主题。要使用一些材料小部件,您必须将应用主题迁移到它。

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2021-11-03
                  • 2016-06-27
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2015-09-22
                  相关资源
                  最近更新 更多