java - 运行时异常 : Font not found for every font i've tried - Android

标签 java android fonts android-assets custom-font

我有一个自定义扩展 TextView,我在我的应用程序中使用自定义字体,但由于某种原因,我不断收到运行时异常,程序无法找到有问题的字体。

我使用的目录格式是 main > assets > fonts > Portrait-Light.ttf

screenshot of directory

我到处寻找解决方案,但他们似乎都在 SO 上四舍五入到相同的答案。

CustomFontTextView.java :

public class CustomFontTextView extends TextView {

    public CustomFontTextView(Context context) {
        super(context);
        applyCustomFont(context);
    }

    public CustomFontTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
        applyCustomFont(context);
    }

    public CustomFontTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        applyCustomFont(context);
    }

    private void applyCustomFont(Context context) {
        Log.e("it gets here", "custom font");
        Typeface customFont = FontCache.getTypeface("Roboto-Italic.ttf", context);
        setTypeface(customFont);
    }
}

字体缓存.java

class FontCache {

    private static HashMap<String, Typeface> fontCache = new HashMap<>();

    static Typeface getTypeface(String fontname, Context context) {
        Typeface typeface = fontCache.get(fontname);
        if (typeface == null) {
            try {
                typeface = Typeface.createFromAsset(context.getAssets(), "fonts/" + fontname);
            } catch (Exception e) {
                Log.e("failed", e.getMessage());
            }
            fontCache.put(fontname, typeface);
        }
        return typeface;
    }
}

XML:

 <icn.premierandroid.misc.CustomFontTextView
            android:id="@+id/switch_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:textSize="14sp"
            android:textColor="@color/colorPrimary"
            android:gravity="center_horizontal"
            android:text="@string/are_you_over_18_years_old"/>

我已经尝试过使用不同的格式,例如 .otf 和不同的字体,例如 Roboto-Italic.ttf 和另一个来自 dafont.com 的随机字体 Sunset-Clouds.ttf 但我仍然收到错误消息,这是怎么回事?这应该工作。为了以防万一,我什至更新了所有插件,例如 Gradle、Grade-Wrapper distribution 和 Android gradle plugin。

我也试过单一的方法:

    AssetManager am = context. getApplicationContext(). getAssets();
    Typeface font = Typeface.createFromAsset(
    am, String.format(Locale.US, "fonts/%s", "portrait-light.ttf"));

我错过了什么吗?

更新:

删除 catch 会显示此堆栈跟踪。

Process: icn.premierandroid, PID: 3829 java.lang.RuntimeException: Unable to start activity ComponentInfo{icn.premierandroid/icn.premierandroid.RegisterActivity}: android.view.InflateException: Binary XML file line #100: Error inflating class icn.premierandroid.misc.CustomFontTextView at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2702) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2767) .....

Caused by: android.view.InflateException: Binary XML file line #100: Error inflating class icn.premierandroid.misc.CustomFontTextView at android.view.LayoutInflater.createView(LayoutInflater.java:640) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750) at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)

.....

Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:288) at android.view.LayoutInflater.createView(LayoutInflater.java:614)

....

Caused by: java.lang.RuntimeException: Font asset not found fonts/GleegooRegular.ttf at android.graphics.Typeface.createFromAsset(Typeface.java:272) at icn.premierandroid.misc.FontCache.getTypeface(FontCache.java:21) at icn.premierandroid.misc.CustomFontTextView.applyCustomFont(CustomFontTextView.java:28) at icn.premierandroid.misc.CustomFontTextView.(CustomFontTextView.java:18) at java.lang.reflect.Constructor.newInstance(Native Method) 

.....

更新 2:

好的,所以有一个奇怪的解决方法。显然 android studio 不喜欢将 assets 文件夹直接添加到 app/src/main 内的 main 文件夹中。您需要先将其添加到app/src/main/res 文件夹中,然后将其移动到main 文件夹中。不知道为什么会这样,但它解决了我的问题。

最佳答案

我认为真正的问题是您没有配置 Assets 文件夹。如果你有,它在项目 View 中看起来像这样:

Asset folder with pictogram

或者在 Android View 中像这样:

Asset folder with pictogram, Android view

所以您应该简单地将此文件夹添加为 Studio 中的 Assets 文件夹: 单击您的项目结构窗口(或按 Alt+1),然后按 Alt + Insert 并选择 Folder/Assets Folder。然后把你的字体放在那里。

来自 OP 的更新:

Okay, so there is a weird work-around for this. Apparently android studio doesn't like it when you add the assets folder straight into the main folder inside app/src/main. You need to add it into the app/src/main/res folder first and then move it to the main folder. Don't have a clue why it's like this but it solved my problem.

关于java - 运行时异常 : Font not found for every font i've tried - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40587698/

相关文章:

java - 我怎样才能压缩我的 XML 和 Java 代码以使我的应用程序运行更流畅

html - 如何垂直对齐图标字体的文本?

java - 添加第二个节点后 Cassandra 中的持续超时

android - @NonNull 注释在 Android Studio 中不起作用

Android App在安装时不要求权限

android - 错误: Gradle DSL method not found: compile()

css - 通过 CSS 向 Wordpress 主题添加自定义字体

javascript - 如何在 Nodejs 服务器上安装我自己的字体?

java - 在 ant 任务中使用 eclipse 类路径

java - Linux 中如何处理键盘事件?