问题
Here is my code and screenshot I'm trying to set custom font typeface but Runtime exception occurs font asset not found while font file is in asset folder. Am I missing something ?
Typeface font = Typeface.createFromAsset(getAssets(), "font/terminal.ttf");
((TextView) findViewById(R.id.weatherHeadingTV)).setTypeface(font);
回答1:
the folder name has to be 'fonts' not 'font'
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/" + font);
回答2:
Your font asset folder is named incorrectly. You should name the folder as fonts
not as font
. Also change your code:
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/terminal.ttf");
回答3:
- Folder's name should be "fonts" and not "font"
- Note that your "fonts" folder is located under your "assets" folder (which should be located under your "main" folder and not your "res" folder) It took me way too long to figure this one out...
回答4:
If you're using Instant Run with Android Gradle plugin version 2.2.0-alphaX, it is a known bug.
A workaround is to turn of Instant Run until the issue is resolved.
You can track it here: https://code.google.com/p/android/issues/detail?id=212849&can=1&q=subcomponent%3DTools-Studio%20-has%3Aproject%20attachments%3D0&colspec=ID%20Status%20Priority%20Owner%20Summary%20Stars%20Reporter%20Opened&start=7700
回答5:
Common error when you have assets in your project and you are using the alpha versions of AS. This appears to be a bug in the Android studio build system. A simple workaround is to clean the project before you run it and that should solve the issue that you are facing.
回答6:
use this method :
final Typeface typeface = ResourcesCompat.getFont(context, R.font.X);
回答7:
I have tried another font file that worked fine So I conclude that earlier font file was corrupt. Thanks @Miduhun MP , @Gowtham Raj and @jagan reddy
回答8:
I had the same problem and managed to fix it. Originally I thought the font files were corrupt but they weren't. Then I thought Android Studio didn't like .ttf
files, because they were the only ones not working. Turns out it's nothing wrong with the fonts.
FIX: Just click Build > Clean project
. Fixed it straight away for me.
回答9:
Typeface typeface = Typeface.createFromAsset(this.getAssets(),"font/terminal.ttf");
((TextView) findViewById(R.id.weatherHeadingTV)).setTypeface(typeface);
回答10:
If you use AndroidAnnotations, in app build.gradle, verify if assets folder is ok: ex: main/src/assets.
If you change de font, uninstall your app from your device/emulator, and run again.
Code:
public static void setFontFace(Context context, TextView textView) {
Typeface type = Typeface.createFromAsset(context.getAssets(), "myfont.ttf");
textView.setTypeface(type);
}
回答11:
I had the problem that .woff fonts are not accepted on Android 7+. So i switched to .ttf fonts.
回答12:
For me, the font file itself was corrupted. I tried another one to make it work.
回答13:
I have looked into all the answer but none of them worked for me. I found a new solution after reading the documentation. Here are the steps to follow:
- Go to file menu
- In new, go to Folder and create assets folder
- Paste your font file in this assets folder
Use in your code using Typeface attribute.
Typeface type = Typeface.createFromAsset(getAssets(), "myfont.ttf"); textView.setTypeface(type);
Now, you are all set to use the fonts you like.
来源:https://stackoverflow.com/questions/33888127/android-runtime-exception-font-asset-not-found