简体   繁体   English

在Android上添加自定义字体时崩溃

[英]Crash on adding custom fonts on android

I have followed this SO (excepted answer) and added FontAwesome.otf to assets/fonts folder here are my strings: 我跟了这SO (答案除外),并加入FontAwesome.otfassets/fonts文件夹,这里是我的字符串:

<!-- icons -->
    <string name="list_icon">&#xf03a;</string>
    <string name="plus_icon">&#xf067;</string>
    <string name="search_icon">&#xf002;</string>

Here are the buttons: 以下是按钮:

<Button
            android:id="@+id/b1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/navigation_button"
            style="?android:attr/buttonBarButtonStyle" 
            android:textColor="#FFFFFF"
            android:text="@string/search_icon" />

        <Button
            android:id="@+id/b2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/navigation_button"
            style="?android:attr/buttonBarButtonStyle"
            android:textColor="#FFFFFF"
            android:text="@string/plus_icon" />

        <Button
            android:id="@+id/b3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@drawable/navigation_button"
            style="?android:attr/buttonBarButtonStyle"
            android:textColor="#FFFFFF" 
            android:text="@string/list_icon" />

and here is the typface: 这是字体:

font = Typeface.createFromAsset(getAssets(), "/fonts/FontAwesome.otf" );

and here is the buttons again: 这又是按钮:

        B1 = (Button) findViewById(R.id.b1);
        B2 = (Button) findViewById(R.id.b2);
        B3 = (Button) findViewById(R.id.b3);

        B1.setTypeface(font);
        B2.setTypeface(font);
        B3.setTypeface(font);

Here is the logcat: 这是logcat:

01-28 00:15:34.863: E/AndroidRuntime(1933): FATAL EXCEPTION: main
01-28 00:15:34.863: E/AndroidRuntime(1933): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.evapp.activities/com.evapp.activities.HomeActivity}: java.lang.RuntimeException: native typeface cannot be made
01-28 00:15:34.863: E/AndroidRuntime(1933):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-28 00:15:34.863: E/AndroidRuntime(1933):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-28 00:15:34.863: E/AndroidRuntime(1933):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-28 00:15:34.863: E/AndroidRuntime(1933):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-28 00:15:34.863: E/AndroidRuntime(1933):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-28 00:15:34.863: E/AndroidRuntime(1933):     at android.os.Looper.loop(Looper.java:137)
01-28 00:15:34.863: E/AndroidRuntime(1933):     at android.app.ActivityThread.main(ActivityThread.java:4745)
01-28 00:15:34.863: E/AndroidRuntime(1933):     at java.lang.reflect.Method.invokeNative(Native Method)
01-28 00:15:34.863: E/AndroidRuntime(1933):     at java.lang.reflect.Method.invoke(Method.java:511)
01-28 00:15:34.863: E/AndroidRuntime(1933):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-28 00:15:34.863: E/AndroidRuntime(1933):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-28 00:15:34.863: E/AndroidRuntime(1933):     at dalvik.system.NativeStart.main(Native Method)
01-28 00:15:34.863: E/AndroidRuntime(1933): Caused by: java.lang.RuntimeException: native typeface cannot be made
01-28 00:15:34.863: E/AndroidRuntime(1933):     at android.graphics.Typeface.<init>(Typeface.java:175)
01-28 00:15:34.863: E/AndroidRuntime(1933):     at android.graphics.Typeface.createFromAsset(Typeface.java:149)
01-28 00:15:34.863: E/AndroidRuntime(1933):     at com.evapp.activities.HomeActivity.onCreate(HomeActivity.java:19)
01-28 00:15:34.863: E/AndroidRuntime(1933):     at android.app.Activity.performCreate(Activity.java:5008)
01-28 00:15:34.863: E/AndroidRuntime(1933):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-28 00:15:34.863: E/AndroidRuntime(1933):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)

这是字体文件的屏幕截图:

What is wrong with that?? 那怎么了?

I found differences with the lines of code below after looking at your referenced link. 在查看您引用的链接后,我发现下面的代码行有所不同。 It seems like the referenced link just used the filename as a parameter whereas you are providing a path to the file. 似乎引用的链接只是使用文件名作为参数,而您正在提供文件的路径。 Also perhaps you want to declare font as a Typeface unless this is done earlier on in the code. 另外,也许您想将字体声明为字体,除非在代码的前面完成了此操作。

Your code... 您的代码...

font = Typeface.createFromAsset(getAssets(), "/fonts/FontAwesome.otf" );

Code from referenced link... 来自引用链接的代码...

Typeface font = Typeface.createFromAsset( getAssets(), "fontawesome-webfont.ttf" );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM