Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Font asset not found helvetica.ttf on lollipop

For some reason on lollipop, I'm getting this error and since lollipop is so new I can't find out why anywhere and I have no idea how to figure it out. Thanks in advance Logcat error:

java.lang.RuntimeException: Unable to start activity                                       
ComponentInfo{com.bent.MissionaryTracker/com.bent.MissionaryTracker.MainActivity}:          
java.lang.RuntimeException: Font asset not found helvetica.ttf
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.RuntimeException: Font asset not found helvetica.ttf
at android.graphics.Typeface.createFromAsset(Typeface.java:190)
at com.bent.MissionaryTracker.MainActivity.onCreate(MainActivity.java:57)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
... 10 more

EDIT: HERE IS THE CODE THAT THROWS THE ERROR

    title = (TextView) findViewById(R.id.title);


    Typeface font = Typeface.createFromAsset(getAssets(), "helvetica.ttf");
    title.setTypeface(font);

I have helvetica.ttf in my assets folder in my project folders.

EDIT: This app works on all devices up until 5.0 so for some reason 5.0 is not recognizing the file in my assets folder.

I tried to post a screenshot of it in my assets folder but I don't have enough reputation to post images.

like image 743
Sloganho Avatar asked Nov 18 '14 05:11

Sloganho


3 Answers

If you are working on Android Studio make sure your asset folder is under main and not res This worked for me

like image 108
Sagar Devanga Avatar answered Nov 11 '22 10:11

Sagar Devanga


Now it's better to use for custom fonts this approach

Store fonts in res/font

To get typeface use :

Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);
like image 76
Penzzz Avatar answered Nov 11 '22 11:11

Penzzz


I had this problem too before I realized I didn't put "fonts/" before the .otf file

 Typeface font = Typeface.createFromAsset(getAssets(),"fonts/TTMastersDEMOBlack.ttf");
like image 33
keredniy Avatar answered Nov 11 '22 11:11

keredniy