Pau*_*nik 39 xml android exception gradle android-studio
您好亲爱的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)
Run Code Online (Sandbox Code Playgroud)
我弄清楚了哪个属性无法解决:
<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>
Run Code Online (Sandbox Code Playgroud)
它位于android-design-library中的res/values/styles/styles.xml中
我在这篇文章中读过API lvl应该是21+.但是由于设计库支持API 7+,实际上这应该不是问题.
值得一提的是,我没有将设计库包含为这样的gradle依赖:
compile 'com.android.support:design:22.2.0'
Run Code Online (Sandbox Code Playgroud)
我正在手动将库添加到项目中,因为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'
}
}
Run Code Online (Sandbox Code Playgroud)
如果有人可以帮助我会很棒.
Kev*_*ant 32
我自己也遇到了这个问题.这是因为我的应用程序尚未使用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>
Run Code Online (Sandbox Code Playgroud)
ori*_*ium 22
有相同的问题,因为已经使用getApplicationContext()
而不是Activity
检索LayoutInflater
和膨胀列表适配器的项目视图.
小智 5
确保您的主题。
我的主题:
<!-- 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>
Run Code Online (Sandbox Code Playgroud)
我在 attrs.xml 和 customview 中使用了自定义属性。我遇到了这个问题,因为我没有theme:
在自定义视图中指定属性。快速查看文件的外观
属性文件
<resources>
<attr name="textColorPrimary" format="reference" />
...
</resources>
Run Code Online (Sandbox Code Playgroud)
自定义视图.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" />
Run Code Online (Sandbox Code Playgroud)
在我的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>
Run Code Online (Sandbox Code Playgroud)
这里的罪魁祸首是自定义属性?textColorPrimary。由于我没有在 customview 中定义 AppTheme,它无法确定如何加载textColorPrimary。通过android:theme="@style/AppTheme",这得到了修复))
归档时间: |
|
查看次数: |
47761 次 |
最近记录: |