Bootstrap

【android】报错java.lang.RuntimeException: Font asset not found fonts/xxx.ttf解决

一、错误描述

1 报错信息

E/AndroidRuntime: FATAL EXCEPTION: main
Process: xxx, PID: 19963
java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx/xxx.activity.provider.WebViewActivity}: java.lang.RuntimeException: Font asset not found fonts/xxx.ttf
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4060)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4247)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:91)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:149)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:103)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2613)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:219)
at android.app.ActivityThread.main(ActivityThread.java:8668)
at java.lang.reflect.Method.invoke(Native Method)

2 报错截图

报错截图

二、报错原因

报错信息为java运行时报错,assets包下未找到fonts/xxx.ttf文件。
在项目升级至androidX版本之后,引入了另一个已升级至新版本的fontUtils.java文件,其中引用了assets/fonts包下的iconfont.ttf样式文件(原先即存在),检查对应部分代码,发现系引入样式文件名称不对应的问题所导致的报错,修改引入样式文件名称即可解决。
代码结构截图

三、代码展示

1 原代码
	static private String Icon_FileName = "xxx_iconfont.ttf";
	static public Drawable getDrawable(Context context, String icon, int size, int color) {
        String TTF_FILE = Icon_FileName;
        //'\uf1d6'
        Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + TTF_FILE);
        Drawable drawable = new IconicsDrawable(context)
                .typeface(typeface)
                .icon(icon)
                .color(color)
                .sizeDp(size);
        return drawable;
    }
2 新代码
	static private String Icon_FileName = "iconfont.ttf";
	static public Drawable getDrawable(Context context, String icon, int size, int color) {
        String TTF_FILE = Icon_FileName;
        //'\uf1d6'
        Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + TTF_FILE);
        Drawable drawable = new IconicsDrawable(context)
                .typeface(typeface)
                .icon(icon)
                .color(color)
                .sizeDp(size);
        return drawable;
    }

四、结果展示

运行成功
结果展示

;