Java 未找到 Android 运行时异常字体资产
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33888127/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Runtime Exception font asset not found
提问by Muzammil Husnain
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);
采纳答案by Muzammil Husnain
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
我尝试了另一个工作正常的字体文件,所以我得出结论,早期的字体文件已损坏。感谢@Miduhun MP、@Gowtham Raj 和@jagan reddy
回答by Gowtham Raj
the folder name has to be 'fonts'not 'font'
文件夹名称必须是“字体”而不是“字体”
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/" + font);
回答by Midhun MP
Your font asset folder is named incorrectly. You should name the folder as fonts
not as font
. Also change your code:
您的字体资源文件夹命名不正确。您应该将该文件夹命名fonts
为font
. 还要更改您的代码:
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/terminal.ttf");
回答by jagan reddy
Typeface typeface = Typeface.createFromAsset(this.getAssets(),"font/terminal.ttf");
字体 typeface = Typeface.createFromAsset(this.getAssets(),"font/terminal.ttf");
((TextView) findViewById(R.id.weatherHeadingTV)).setTypeface(typeface);
((TextView) findViewById(R.id.weatherHeadingTV)).setTypeface(typeface);
回答by Lauren?iu Onac
If you're using Instant Run with Android Gradle plugin version 2.2.0-alphaX, it is a known bug.
如果您将 Instant Run 与 Android Gradle 插件版本 2.2.0-alphaX 一起使用,这是一个已知错误。
A workaround is to turn of Instant Run until the issue is resolved.
解决方法是关闭 Instant Run,直到问题得到解决。
您可以在此处跟踪: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
回答by Jishin Dev
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.
当您的项目中有资产并且您使用的是 AS 的 alpha 版本时,会出现常见错误。这似乎是 Android Studio 构建系统中的一个错误。一个简单的解决方法是在运行之前清理项目,这应该可以解决您面临的问题。
回答by Pierry
If you use AndroidAnnotations, in app build.gradle, verify if assets folder is ok: ex: main/src/assets.
如果您使用 AndroidAnnotations,请在 app build.gradle 中验证资产文件夹是否正常:例如: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);
}
回答by bremen_matt
I had the problem that .woff fonts are not accepted on Android 7+. So i switched to .ttf fonts.
我遇到的问题是 Android 7+ 不接受 .woff 字体。所以我切换到 .ttf 字体。
回答by Will
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.
我遇到了同样的问题并设法解决了它。最初我认为字体文件已损坏,但事实并非如此。然后我认为 Android Studio 不喜欢.ttf
文件,因为它们是唯一不起作用的文件。结果发现字体没有问题。
FIX: Just click Build > Clean project
. Fixed it straight away for me.
修复:只需单击Build > Clean project
。直接帮我修好了。
回答by Bilal Halayqa
For me, the font file itself was corrupted. I tried another one to make it work.
对我来说,字体文件本身已损坏。我尝试了另一个使其工作。