You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中FButton膨胀失败致应用崩溃求助

解决FButton Inflate错误:Can't convert to color: type=0x5

这个错误的核心原因是FButton无法正确解析你设置的颜色值,通常是颜色引用方式不对,或者库与你的Android Support版本不兼容导致的。下面是针对性的解决方案:

1. 检查colors.xml中的颜色定义

首先确认你的res/values/colors.xml里,colorPrimary和colorPrimaryDark是具体的十六进制颜色值,而不是主题属性引用:

正确的示例:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <!-- 其他颜色定义 -->
</resources>

如果你的colors.xml里是这种写法,请改成上面的具体颜色值:

<color name="colorPrimary">?attr/colorPrimary</color>

2. 解决Support库与FButton版本不兼容问题

你使用的info.hoang8f:fbutton:1.0.5是一个比较旧的库,它的内部实现可能不支持Android Support Library 26+的颜色系统。可以通过两种方式解决:

方式一:降级Android Support库版本

修改你的Module级别build.gradle,将所有Support库版本从26.+降级到25.3.1(FButton官方兼容的最高版本):

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    testCompile 'junit:junit:4.12'
    //Add Library
    compile 'com.android.support:recyclerview-v7:25.3.1'
    compile 'com.android.support:cardview-v7:25.3.1'
    compile 'info.hoang8f:fbutton:1.0.5'
}

方式二:替换为兼容的按钮库(备选)

如果不想降级Support库,可以考虑使用更活跃的替代库——AndroidX原生的MaterialButton,它完全兼容高版本的Support/AndroidX库,用法类似:

  1. 先迁移到AndroidX(如果还没完成)
  2. 在布局中替换info.hoang8f.widget.FButton为com.google.android.material.button.MaterialButton
  3. 调整属性为MaterialButton对应的属性,示例:
<com.google.android.material.button.MaterialButton
    android:id="@+id/btnExercise"
    android:text="Exercises"
    android:textColor="@android:color/white"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="4dp"
    android:layout_weight="1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:backgroundTint="@color/colorPrimary"
    app:elevation="2dp"
    app:cornerRadius="2dp" />

3. 确认主题兼容性

确保你的AndroidManifest.xml中,Application或MainActivity的主题继承自Theme.AppCompat(或其子类),比如:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

对应的styles.xml中AppTheme定义:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

完成以上步骤后,执行Clean Project再Rebuild Project,应该就能解决启动崩溃的问题了。

内容的提问来源于stack exchange,提问作者Alfin H

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:48:18