Android “无法制作原生字体”仅适用于某些人

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12766930/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 12:01:59  来源:igfitidea点击:

"Native typeface cannot be made" only for some people

androidfontstypeface

提问by user936580

I have an app that changes the font typeface for some elements. It works well for most of the people, but maybe a 0.5% get an exception when trying to change the font. The significant part of the stack trace is this:

我有一个应用程序可以更改某些元素的字体。它适用于大多数人,但可能有 0.5% 的人在尝试更改字体时会出现异常。堆栈跟踪的重要部分是这样的:

Caused by: java.lang.RuntimeException: native typeface cannot be made
at android.graphics.Typeface.<init>(Typeface.java:147)
at android.graphics.Typeface.createFromAsset(Typeface.java:121)

As I say, it works for most of the people, so I don't think it is a problem with the font file or my code. Any suggestions about how to solve this?

正如我所说,它适用于大多数人,所以我认为这不是字体文件或我的代码的问题。有关如何解决此问题的任何建议?

Edit:This is my code:

编辑:这是我的代码:

Typeface phoneticFont = Typeface.createFromAsset(getAssets(),
                                                 "fonts/CharisSILR.ttf");
TextView tv;
tv = ((TextView) findViewById(R.id.searchPronunciationTitle));
tv.setTypeface(phoneticFont);

回答by HitOdessit

This bug of Android OS could be the reason of your issue:

Android 操作系统的此错误可能是您出现问题的原因:

Typeface.createFromAsset leaks asset stream

Typeface.createFromAsset 泄漏资产流

Where are also a workaround in this bugreport:

此错误报告中的解决方法也在哪里:

I altered HTH's workaround so that the method does not assume the font path or format. The full path of the font asset must be submitted as a parameter. I also wrapped the call to createFromAsset() in a try-catch block so that the get() method will return null if the asset is not found.

我更改了 HTH 的解决方法,以便该方法不采用字体路径或格式。字体资产的完整路径必须作为参数提交。我还将对 createFromAsset() 的调用封装在 try-catch 块中,以便在未找到资产时 get() 方法将返回 null。

public class Typefaces {
    private static final String TAG = "Typefaces";

    private static final Hashtable<String, Typeface> cache = new Hashtable<String, Typeface>();

    public static Typeface get(Context c, String assetPath) {
        synchronized (cache) {
            if (!cache.containsKey(assetPath)) {
                try {
                    Typeface t = Typeface.createFromAsset(c.getAssets(),
                            assetPath);
                    cache.put(assetPath, t);
                } catch (Exception e) {
                    Log.e(TAG, "Could not get typeface '" + assetPath
                            + "' because " + e.getMessage());
                    return null;
                }
            }
            return cache.get(assetPath);
        }
    }
}

回答by joao_dv

I followed some of the solutions found here, with no success. I thought it was something really obscure, as programmers often do. Then somewhere I read it could be related to the font path, gotcha:

我遵循了这里找到的一些解决方案,但没有成功。我认为这是一件非常晦涩的事情,就像程序员经常做的那样。然后我读到的某个地方可能与字体路径有关,请注意:

Instead of:

代替:

Typeface phoneticFont = Typeface.createFromAsset(getAssets(),
                                             "blanch_caps.ttf");   

I changed to:

我改为:

Typeface phoneticFont = Typeface.createFromAsset(getAssets(),
                                             "fonts/blanch_caps.ttf");   

And my file is in assets/fonts/blanch_caps.ttf. Not it works like a charm!

我的文件在 assets/fonts/blanch_caps.ttf 中。不是它像魅力一样!

回答by Lumis

This error came up when the font was in the library asset folder. When I copied it into assets of the application which was using this library, the error disappeared.

当字体位于库资产文件夹中时出现此错误。当我将它复制到使用这个库的应用程序的资产中时,错误消失了。

It seems assets cannot be imported: Android Library assets folder doesn't get copied

似乎无法导入资产Android 库资产文件夹不会被复制

And here are some other cases: Issue when using a custom font - "native typeface cannot be made"

还有一些其他情况:使用自定义字体时出现问题 - “无法制作原生字体”

回答by Michal

I was struggling with this a lot. I tried every possibility and nothing helps. In the end, to problem was somewhere else. If you are building your project with Gradle, don't forget to add these lines in build.gradle file. This solved the problem in my case.

我为此苦苦挣扎。我尝试了所有可能性,但没有任何帮助。最后,问题出在别处。如果您使用 Gradle 构建项目,请不要忘记在 build.gradle 文件中添加这些行。这解决了我的问题。

    sourceSets {
    main {
        assets.srcDirs = ['assets']
    }
}

回答by Madi

You must create assets folder inside src-->mainin AndroidStudio. This way worked!

您必须在 AndroidStudio 的src--> main中创建 assets 文件夹。这种方式奏效了!

回答by Chulo

For my case I have found that the assetsfolder located in /main/java/assetsbut they must be in /main/assets

就我而言,我发现资产文件夹位于/main/java/assets但它们必须位于/main/assets

回答by Tab

In my case, it was based on the filename of the font. For some reason it was named FontName..ttf

就我而言,它基于字体的文件名。出于某种原因,它被命名为 FontName..ttf

I don't know why the double-dots were there - I looked up the original font and they were in my windows\fonts folder as FontName..ttf. Windows apparently didn't care, but Android freaked out. I renamed the file, and it's all happy now.

我不知道为什么会有双点 - 我查找了原始字体,它们在我的 windows\fonts 文件夹中作为 FontName..ttf。Windows 显然不在乎,但 Android 吓坏了。我重命名了文件,现在一切都很愉快。

回答by Pedro HCDO

Do with lower case:

使用小写:

Typeface phoneticFont = Typeface.createFromAsset(getAssets(),
                                             "fonts/charissilr.ttf");

Remember to also rename the file.

记住还要重命名文件。

回答by Kadir alt?nok

Change this one

换这个

Typeface phoneticFont = Typeface.createFromAsset(getAssets(),
                                             "fonts/CharisSILR.ttf");

to

Typeface phoneticFont = Typeface.createFromAsset(getAssets(),
                                             "CharisSILR.ttf");

回答by Sanjeet A

I was ran in to this issue when i imported a module which was meant to support both eclipse style projects and android studio style project.

当我导入一个旨在支持 eclipse 风格项目和 android studio 风格项目的模块时,我遇到了这个问题。

I got my issue resolved by removing the assets from the source set as-

我通过从源集中删除资产解决了我的问题 -

defaultConfig {
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName '1.0'

        sourceSets {
            main {
                java.srcDirs = ['src']
                res.srcDirs = ['res']
                //assets.srcDirs = ['assets']//commented this out because modlue was not having this directory
                manifest.srcFile 'AndroidManifest.xml'
            }
        }
    }

Or converting the project in to android studio style can also solve the issue I guess.

或者将项目转换为android studio样式也可以解决我猜的问题。