Exclude pointer exception when importing font in android

I am trying to import a font. I looked at a few tutorials online and used the instructions. So far, I have imported the font into the resource folder. This is the code that I use to declare my font type.

Typeface localTypeface1 = Typeface.createFromAsset(getAssets(), "arial.ttf"); 

I keep getting a null pointer exception when I call it.

 TextView txtTab = new TextView(this); txtTab.setTypeface(localTypeface1); 

What could be the problem? This is the exact error in logcat.

 11-17 13:10:41.024: E/AndroidRuntime(2262): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{se.copernicus.activity/se.copernicus.activity.Secondactivity}: java.lang.NullPointerException 11-17 13:10:41.024: E/AndroidRuntime(2262): at android.content.ContextWrapper.getAssets(ContextWrapper.java:74) 

This is line 74

 Typeface localTypeface1 = Typeface.createFromAsset(getAssets(), "arial.ttf"); 

enter image description here

+6
source share
3 answers

What I saw, Android cannot read some ttf files. (maybe the file is damaged?)

I was unable to use the ttf file in my application, but another application in my phone could read the same ttf file. (I used the same code as above, tried to clear, replacing it in folders with different folders, nothing worked)

Finished using another font file as a whole. (There was no problem with this file with the same code!)

+2
source

Have you tried moving the font directly to assets? (Not in the font folder) and get rid of the font folder?

+1
source

Try to clean (Project β†’ Clean ...) your project

+1
source

Source: https://habr.com/ru/post/901694/


All Articles