“运行时异常:无法制作本机字体”加载字体时

发布于 2024-09-05 11:24:05 字数 3566 浏览 5 评论 0原文

我正在尝试按照指南

W/dalvikvm(  317): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
E/AndroidRuntime(  317): FATAL EXCEPTION: main
E/AndroidRuntime(  317): java.lang.RuntimeException: Unable to start activity  ComponentInfo{org.evilx.quacklock/org.evilx.quacklock.MainActivity}:             java.lang.RuntimeException: native typeface cannot be made
E/AndroidRuntime(  317):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime(  317):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime(  317):        at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime(  317):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime(  317):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  317):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  317):        at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(  317):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  317):        at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  317):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(  317):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(  317):        at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  317): Caused by: java.lang.RuntimeException: native typeface cannot be made
E/AndroidRuntime(  317):        at android.graphics.Typeface.<init>(Typeface.java:147)
E/AndroidRuntime(  317):        at android.graphics.Typeface.createFromAsset(Typeface.java:121)
E/AndroidRuntime(  317):        at org.evilx.quacklock.MainActivity.onCreate(MainActivity.java:24)
E/AndroidRuntime(  317):        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(  317):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime(  317):        ... 11 more
W/ActivityManager(   59):   Force finishing activity org.evilx.quacklock/.MainActivity
W/ActivityManager(   59): Activity pause timeout for HistoryRecord{43e80368 org.evilx.quacklock/.MainActivity}
D/dalvikvm(  247): GC_EXPLICIT freed 711 objects / 53160 bytes in 20922ms

我正在使用字体 Molot.otf,它已在其中一个博客中成功使用。我还使用 predator.ttf,这是另一种自定义字体,但采用 TrueType 格式。

相关代码:

public class MainActivity extends Activity {
    // Called when the activity is first created.
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Molot.otf");
        TextView tv = (TextView) findViewById(R.id.CustomFontText);
        tv.setTypeface(tf);
    }
}

什么

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/CustomFontText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="Here is some text.">
    </TextView>
</LinearLayout>

会导致这种情况?它对博客中的人有效,那为什么对我无效呢? API 中是否发生了重大变化,导致我无法执行此操作?

I am attempting to use a custom font for a TextView on Android, following the guide here. Using the same font, same code, same everything, I get this in adb logcat:

W/dalvikvm(  317): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
E/AndroidRuntime(  317): FATAL EXCEPTION: main
E/AndroidRuntime(  317): java.lang.RuntimeException: Unable to start activity  ComponentInfo{org.evilx.quacklock/org.evilx.quacklock.MainActivity}:             java.lang.RuntimeException: native typeface cannot be made
E/AndroidRuntime(  317):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
E/AndroidRuntime(  317):        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
E/AndroidRuntime(  317):        at android.app.ActivityThread.access$2300(ActivityThread.java:125)
E/AndroidRuntime(  317):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
E/AndroidRuntime(  317):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(  317):        at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(  317):        at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(  317):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  317):        at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(  317):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(  317):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(  317):        at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(  317): Caused by: java.lang.RuntimeException: native typeface cannot be made
E/AndroidRuntime(  317):        at android.graphics.Typeface.<init>(Typeface.java:147)
E/AndroidRuntime(  317):        at android.graphics.Typeface.createFromAsset(Typeface.java:121)
E/AndroidRuntime(  317):        at org.evilx.quacklock.MainActivity.onCreate(MainActivity.java:24)
E/AndroidRuntime(  317):        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
E/AndroidRuntime(  317):        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
E/AndroidRuntime(  317):        ... 11 more
W/ActivityManager(   59):   Force finishing activity org.evilx.quacklock/.MainActivity
W/ActivityManager(   59): Activity pause timeout for HistoryRecord{43e80368 org.evilx.quacklock/.MainActivity}
D/dalvikvm(  247): GC_EXPLICIT freed 711 objects / 53160 bytes in 20922ms

I'm using the font Molot.otf, which was successfully used in one of the blogs. I'm also using predator.ttf, another custom font but in TrueType format.

Relevant code:

public class MainActivity extends Activity {
    // Called when the activity is first created.
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Molot.otf");
        TextView tv = (TextView) findViewById(R.id.CustomFontText);
        tv.setTypeface(tf);
    }
}

and

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView
        android:id="@+id/CustomFontText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textSize="30sp"
        android:text="Here is some text.">
    </TextView>
</LinearLayout>

What would be causing this? It worked for the people in the blogs, so why not me? Did something significant change in the API that's preventing me from doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

独自唱情﹋歌 2024-09-12 11:24:05

Android 不支持 OpenType (OTF),仅支持 TrueType (TTF),因此您的 Molot.otf 字体可能无法使用。我写了您在开头句子中链接到的两篇博客文章(其中一篇是另一篇的盗版副本),并且它们不使用 Molot.otf

(顺便说一句,我稍微修复了该帖子的格式——AndroidGuys 不断更改 WordPress 主机,因此我的旧帖子在格式方面严重损坏)。

编辑:正如评论中所述,Android 确实现在支持 OTF。

Android does not support OpenType (OTF), only TrueType (TTF), so your Molot.otf font probably will not work. I wrote both of those blog posts you link to in your opening sentence (the one is a pirated copy of the other), and they do not use Molot.otf.

(BTW, I somewhat repaired the formatting of that post -- AndroidGuys keeps changing WordPress hosts, and so my older posts are terribly broken in terms of formatting).

EDIT: As stated in the comments, Android DOES now support OTF.

篱下浅笙歌 2024-09-12 11:24:05

我也遇到同样的错误,并且我有解决方案。

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Molot.otf")

您必须将 fonts/Molot.otf 放入 Eclipse 的 assets/fonts 文件夹中。

之后就可以运行它了。

如果您无法成功运行它,您可以通过以下方式发送问题

I also get the same error and I have solution follow.

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/Molot.otf")

you must put fonts/Molot.otf in assets/fonts folder in your Eclipse.

after you can Run it.

if you can not Run it successfull you can send proplem via

拥抱我好吗 2024-09-12 11:24:05

不幸的是,无法制作字体错误并不是很具体,它可能是许多事情出错的结果。检查两件事很重要:

  • 第一件事也是最重要的:文件已找到!
  • 该字体在您的设备中有效。

最好的方法是将字体文件更改为已知的有效字体文件。
如果失败了,那就是第一个问题。
如果没有,那就是第二种字体,因此您将不得不处理 FontForge 或寻找其他字体。

Unfortunately, the typeface cannot be made error is not very specific, and it can be the result of many things going wrong. It's important to check for two things:

  • The first and most important: The file is found!
  • The font is valid in your device.

The best way is to change your font file for a known valid font file.
If it fails, then it's the first problem.
If not, it's the second, so you will have to deal with FontForge or look for another font.

绝不服输 2024-09-12 11:24:05

检查字体的名称和扩展名。它区分大小写 &可能全部大写。例如。

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/MOLOT.OTF")

check font's name and extension. it is case sensitive & probably all caps. eg.

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/MOLOT.OTF")
昨迟人 2024-09-12 11:24:05

供参考。
我的崩溃原因是 Eclipse 引起的。
我所做的只是清理项目并再次运行,然后它就可以工作了。

首先,我在我的测试项目中尝试了自定义字体,我用它来尝试一些新功能。它第一次就工作了。但直到我按照上面的方法做之前,它才对我正在从事的项目起作用。

因此,请尝试尽可能多的方法。

FYI.
My reason for the crash is some reason caused by Eclipse.
All I did is just cleaning the project and ran again, then it works.

Firstly, I tried the custom font in my test project which I use to try some new functions.It worked on the very first time. But it didn't work on the project i'm working on until I did as above.

So try as many methods as you can.

白鸥掠海 2024-09-12 11:24:05

@deng 他的答案对我有用”:

检查字体的名称和扩展名。它区分大小写,并且可能全部大写。例如。

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/MOLOT.OTF")

@deng his answer worked for me":

check font's name and extension. it is case sensitive & probably all caps. eg.

Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/MOLOT.OTF")
江南月 2024-09-12 11:24:05

您应该使用“Fontlab”软件以二进制模式编辑字体。

You should edit your font with 'Fontlab' Software in binary mod.

行雁书 2024-09-12 11:24:05

Android 确实支持 Typefaces 的 OTF 文件,如果您遇到此问题,请确保为字体设置正确的路径,例如,如果您有文件 fontname.otf,请将其放在 asset 内的 fonts 文件夹中文件夹并创建字体,如下所示:(

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/fontname.otf");

路径参数不应以“/”开头)并且文件名不应包含特殊字符或“-”并且应为小写

Android does support OTF files for Typefaces, if you're facing this problem, make sure that you're setting the right path for the font, for example, if you have the file fontname.otf, put it in a folder fonts inside assets folder and create the typeface like follows :

Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/fontname.otf");

(path argument should not start with "/") and the file name should not include special characters or a "-" and should be in lowercases

倾其所爱 2024-09-12 11:24:05

Android 确实支持字体的 OTF 文件,如果您遇到此问题,请确保为字体设置正确的路径。将字体放入 asset 文件夹内的文件夹 fonts 中,并创建字体,如下所示:

Typeface typeface = Typeface.createFromAsset(getAssets(), "font/StencilStd.otf");
TextView text = (TextView) findViewById(R.id.textView);
text.setTypeface(typeface);

Android does support OTF files for Typefaces, if you're facing this problem, make sure that you're setting the right path for the font.put font into folder fonts inside assets folder and create the typeface as below :

Typeface typeface = Typeface.createFromAsset(getAssets(), "font/StencilStd.otf");
TextView text = (TextView) findViewById(R.id.textView);
text.setTypeface(typeface);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文