Android: Exception in setting Custom Font

2019-06-26 03:13发布

问题:

I have been striking my head with setting up custom fonts to text from code since one hour.I already deed this in my previous project and it worked!

But I don't know for what reason,it gives me expection "native typeface cannot be made" here.I have gone through many questions on SO related to this,and tried solution suggested and worked for those cases.But none of them works for me. :(

I have deleted and recreated assests folder and also i have tried with other font.ttf file as per the answers on the question related to same issues.I also cleaned and rebuilded the project but every effort was in vain!

Please help me!! I will be grateful for your help.

Code i use to set fonts:

I have my fonts GOTHIC.TTF in assets folder:

...
Typeface font=Typeface.createFromAsset(getAssets(), "fonts/GOTHIC.TTF");
title.setTypeface(font);
...

EDIT :

I think,I was mistaking by writing "fonts/GOTHIC.TTF" where as i have pasted fonts directly in assets and not in assets/fonts.But making it "GOTHIC.TTF" also does not solve the issue.Where can I be wrong?!

回答1:

Where are the fonts place and are they referenced correctly?

example:

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

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

see:
"RuntimeException: native typeface cannot be made" when loading font



回答2:

Make sure that the font extension is all lower case. In other words it should not be

GOTHIC.TTF

but

GOTHIC.ttf

Also tryputting it in the fonts folder in assets and reference it using fonts/GOTHIC.TTF



回答3:

I don't know what exactly worked from all I tried for but I got it working! :)

All I tried was,closing eclipse and immulator.Re-opening the project,cleaning the same and then fixing other bug of my project resulted me the working one.

Thanks for all the help guys!!