Android XML:RuntimeException:未能解析索引6处的属性

Android XML:RuntimeException:未能解析索引6处的属性,android,xml,exception,android-studio,gradle,Android,Xml,Exception,Android Studio,Gradle,你好,亲爱的stackoverflower 在我的项目中,我正在使用新的“android设计库”。 问题是,有一个运行时异常,它说(我正在尝试创建一个FloatingButton): 我正在将库手动添加到项目中,因为Jenkins服务器无法访问Internet。 我已将support-v4库更新为21.2.0 此外,还包括并更新了appcompat support-v7 以下是android设计库gradle文件: android { compileSdkVersion 21 buildTool

你好,亲爱的stackoverflower

在我的项目中,我正在使用新的“android设计库”。 问题是,有一个运行时异常,它说(我正在尝试创建一个FloatingButton):

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

以下是android设计库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'
    }
}
compile 'com.android.support:design:22.2.0'

如果有人能帮助我就太好了。

我自己也遇到了这个问题。这是因为我的应用程序还没有使用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>

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

我的主题:

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

#2196F3
#1565C0
#E91E63

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

在app build.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'
    }
}
compile 'com.android.support:design:22.2.0'

由于使用了
getApplicationContext()
而不是
Activity
来检索列表适配器的
LayoutInflater
和充气项视图,因此出现了相同的问题。

在我的情况下,我在
setContentView(R.layout.my\u layout)
中出错。
my_layout
中,我在
TextInputLayout
中使用了导致错误的
app:errorEnabled=“true”
。删除了该行,它就工作了。

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

attrs.xml


...
customview.xml


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

@颜色/黑色
...
20便士
无衬线灯
正常的
?textColorPrimary
...

罪魁祸首是自定义属性?textColorPrimary。由于我没有在customview中定义AppTheme,它无法确定如何加载textColorPrimary。通过android:theme=“@style/AppTheme”,这个问题得到了解决”)

对于我的情况,这个问题在迁移到AndroidX后开始出现。该场景是我实现的自定义视图,它扩展了
appcompatiedittext
,并设置了自定义样式。我在运行单元测试时遇到了这个问题

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

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的base
Android:Widget.EditText
删除了错误,并且没有为我更改任何行为/UI


奇怪的是,通过挖掘
Base.Widget.AppCompat.EditText的继承结构,根父级也是
android:Widget.EditText

,以防任何人在设置android TV/与AndroidTV示例代码一起工作时遇到此问题,然后,解决方案是将leanback主题添加到清单中的活动中

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


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

不幸的是,我的应用程序没有
colorAccent
属性,因为我没有使用AppCompat。我发布的解决方案应该适用于具有与我描述的类似设置的开发人员。。。你是最棒的。。3天来我的头一直在痛。。你解决了问题,这确实解决了我的问题。我创建了一个自定义文本视图,并在布局中使用该自定义视图。工作正常,直到我在应用程序清单中更改了应用程序主题并开始看到此错误。将其更改回Theme.AppCompat.Light解决了此问题。我假设我选择的备用主题没有我在样式中使用的一些主题元素。这就是答案。有人能解释一下原因吗?这也是我的答案。为了澄清,我将
PopupMenu fileMenu=new PopupMenu(getApplicationContext(),fileButton)
更改为
PopupMenu fileMenu=new PopupMenu(main activity.this,fileButton)
一切正常。
?…
是我的问题
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)
<style name="MyEditText" parent="Base.Widget.AppCompat.EditText">
    <item name="android:fontFamily">...</item>
    <item name="android:textSize">...</item>
</style>
<activity
  android:name=".PlaybackActivity"
  android:theme="@style/Theme.Leanback" />