简体   繁体   中英

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:

<!-- 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:

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" );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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