Android “font asset not found” error

匆匆过客 提交于 2020-01-02 07:42:50

问题


I am developing for Android version 22.

I am getting the runtime exception "Font asset not found"

Here is the code:

Typeface customFont =    Typeface.createFromAsset(getAssets(), "fonts/norwester.otf");

And here is my file structure:

I am guessing that my file structure is wrong because the "assets" folder does not show up in "Android" mode and only shows up in "Project Mode".

What is the problem?


回答1:


Assets folder should be under root directory of main folder which inner folder of src.

For Reference Please find attached images of folder structure.




回答2:


I am guessing that my file structure is wrong because the "assets" folder does not show up in "Android" mode and only shows up in "Project Mode"

According to your screenshots, you do not have an "assets" folder. You have a res/assets.fonts/ folder. It has the wrong name (assets.fonts, not assets) and is in the wrong location (a child of res/).

Step #1: In your project tree, right-click over main.

Step #2: Choose New > Directory from the context menu that appears when you right-clicked.

Step #3: In the resulting dialog, type assets, then click OK. You will now have an assets/ directory under main.

Step #4: In your project tree, right-click over your newly-created assets directory.

Step #5: Choose New > Directory from the context menu that appears when you right-clicked.

Step #6: In the resulting dialog, type 'fonts', then click OK. You will now have an assets/fonts/ directory under main.

Step #7: Drag your font file from its current location to move it into assets/fonts/.

Step #8: Delete the res/assets.fonts/ directory, which is now empty.




回答3:


I am guessing that my file structure is wrong because the "assets" folder does not show up in "Android" mode and only shows up in "Project Mode".

Your assets is empty you can't see it in "Android mode". Change mode to "Project" and add your font to assets folder.

Right location of assets folder:

root-module
|--.idea
|--app
|----build
|----src
|------main
|--------assets
|----------norwester.otf
|--------java
|----------source code here
|--------res
|------AndroidManifest.xml
|----build.gradle

To create assets folder Right click on app->New->Folder->Assets Folder like below image.




回答4:


The assets folder must be in the main folder, not in resources folder. Also there is assets.fonts in your project which I guess is not the correct format. You can create the assets in the main directory, then right click -> New -> Directory and give its name as fonts

Hope this helps.



来源:https://stackoverflow.com/questions/36224002/android-font-asset-not-found-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!