2

My application is crashing on the following code. I am trying to give a custom font in a textView. The asset directory is fine as I have double checked it.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView title = (TextView) findViewById(R.id.textView);
    String BANGLA_FONT_SOLEMAN_LIPI = "/SolaimanLipi_Bold_10-03-12.ttf";
    Typeface tf = Typeface.createFromAsset(getApplicationContext().getAssets(),
            BANGLA_FONT_SOLEMAN_LIPI);
    title.setTypeface(tf);
}

When the application crashes it produces the following Error Log:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gsl.banglafonttest/com.gsl.banglafonttest.MainActivity}: java.lang.RuntimeException: native typeface cannot be made
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2416)
                                                                                at android.app.ActivityThread.access$600(ActivityThread.java:174)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1382)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:107)
                                                                                at android.os.Looper.loop(Looper.java:194)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5409)
                                                                                at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                at java.lang.reflect.Method.invoke(Method.java:525)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606)
                                                                                at dalvik.system.NativeStart.main(Native Method)
                                                                             Caused by: java.lang.RuntimeException: native typeface cannot be made
                                                                                at android.graphics.Typeface.<init>(Typeface.java:175)
                                                                                at android.graphics.Typeface.createFromAsset(Typeface.java:149)
                                                                                at com.gsl.banglafonttest.MainActivity.onCreate(MainActivity.java:17)
                                                                                at android.app.Activity.performCreate(Activity.java:5122)
                                                                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1150)
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2328)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2416) 
                                                                                at android.app.ActivityThread.access$600(ActivityThread.java:174) 
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1382) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:107) 
                                                                                at android.os.Looper.loop(Looper.java:194) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5409) 
                                                                                at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                at java.lang.reflect.Method.invoke(Method.java:525) 
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:606) 
                                                                                at dalvik.system.NativeStart.main(Native Method) 
Saeed Sharman
  • 765
  • 5
  • 24

4 Answers4

2

There are basically 4 things that can cause this:

1.You use the wrong extension

2.You placed the fonts in the assets folder and not inside assets/fonts/

3.You misspelled the fonts

4.The fonts need to be lowercase (in my case the solution was to rename MyFont.ttf to myfont.ttf, strange)

Veeresh Charantimath
  • 4,641
  • 5
  • 27
  • 36
1

Typeface tf = Typeface.createFromAsset(context.getAssets(), "font/Nunito-Light.ttf"); vh.textView_header.setTypeface(tf,45);

This is a sample i used in my application, the custom font is within the font folder in the android assets directory.

Ask
  • 53
  • 5
1

I would recommend you Calligraphy to get rid of custom TextView or "boilerplate" .setTypeFace

I used to have custom TextView but Calligraphy made my code far cleaner.

Renaud Mathieu
  • 366
  • 2
  • 9
0

The solution in my case is creating a font folder in assets folder and keeping the file there.

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView title = (TextView) findViewById(R.id.textView);
    String BANGLA_FONT_SOLEMAN_LIPI = "font/SolaimanLipi_22-02-2012.ttf";
    Context cn=getApplicationContext();
    Typeface tf = Typeface.createFromAsset(cn.getAssets(),
            BANGLA_FONT_SOLEMAN_LIPI);
    title.setTypeface(tf);
    title.setText(R.string.text_of_title);
}

}

Saeed Sharman
  • 765
  • 5
  • 24