Font asset not found helvetica.ttf on lollipop - java

For some reason on lollipop, I'm getting this error and since lollipop is so new I can't find out why anywhere and I have no idea how to figure it out. Thanks in advance Logcat error:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.bent.MissionaryTracker/com.bent.MissionaryTracker.MainActivity}:
java.lang.RuntimeException: Font asset not found helvetica.ttf
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.RuntimeException: Font asset not found helvetica.ttf
at android.graphics.Typeface.createFromAsset(Typeface.java:190)
at com.bent.MissionaryTracker.MainActivity.onCreate(MainActivity.java:57)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
... 10 more
EDIT: HERE IS THE CODE THAT THROWS THE ERROR
title = (TextView) findViewById(R.id.title);
Typeface font = Typeface.createFromAsset(getAssets(), "helvetica.ttf");
title.setTypeface(font);
I have helvetica.ttf in my assets folder in my project folders.
EDIT:
This app works on all devices up until 5.0 so for some reason 5.0 is not recognizing the file in my assets folder.
I tried to post a screenshot of it in my assets folder but I don't have enough reputation to post images.

If you are working on Android Studio make sure your asset folder is under main and not res
This worked for me

Now it's better to use for custom fonts this approach
Store fonts in res/font
To get typeface use :
Typeface typeface = ResourcesCompat.getFont(context, R.font.myfont);

I had this problem too before I realized I didn't put "fonts/" before the .otf file
Typeface font = Typeface.createFromAsset(getAssets(),"fonts/TTMastersDEMOBlack.ttf");

This worked for me. In my case it didn't work because I created assets folder in res and not in main.

If changing the directory doesn't work it could be due to a problem already fixed by the Android team but didn't make it in time for 5.0. It's all explained here:
https://code.google.com/p/android-developer-preview/issues/detail?id=608&thanks=608&ts=1404735239
The workaround (at least it was for me) is to fix the font files with TTX as mentioned in the linked issue.
Getting TTX to work can be a bit of a hassle if you have no python expertise so I ended up downloading Adobe's AFDKO that includes TTX. You have to run 'ttx yourfontfile.ttf' that'll turn yourfile to .ttx and then run it again 'ttx yourfontfile.ttx' to get a new .ttf that works with 5.0.

Clean the project, it helped me on android 6.0

Just place a assets folder inside main directory.
After do this:
Typeface typeface=Typeface.createFromAsset(getAssets(),"OpenSans-Light.ttf");
textView.setTypeface(typeface);

I faced this issue before. I deleted x.ttf and then undid the deletion, tried to run with no lock. So my fix was just to delete it and downloaded the font again and pasted it to fonts/ folder and every thing worked fine.

Ok Guys,
I ran into the same problem. First of all I made my font loading logic crash proof:
/**
* Init method. This method must be called before any other call in this class.
*/
public static void init () {
fontPlain = createFont( "helvetica.ttf" , Typeface.NORMAL );
fontBold = createFont( "helveticab.ttf", Typeface.BOLD );
}
/**
* Load font from assets font folder.
*/
public static Typeface createFont (String font, int style) {
Typeface typeface;
try {
AssetManager assets = Kit.getAppContext().getAssets();
typeface = Typeface.createFromAsset( assets, "fonts" + File.separator + font );
}
catch (RuntimeException e) {
// createFromAsset() will throw a RuntimeException in case of error.
Log.e( Constants.TAG, "Unable to create font: " + font, e );
typeface = Typeface.defaultFromStyle( style );
}
return typeface;
}
And then I also downloaded another font from this project in git:
https://github.com/dimanchec3/Tutoriapps-Android/blob/master/assets/Helvetica.ttf
I realized that my old font was 56KB and this new font is 84KB. I guess I had an older version of the font.
Please make sure you have the proper license before using this font.

Related

How to change a photo?

I am trying to change photos in android studio by clicking on my button.
When I put code for changing the photo in my MainActivity.java I keep getting this type of error messages and it says :
Cannot resolve symbol "image"
image.setImageResource(R.drawable.xxx);
I am watching Udemy course for android development and I have done everything same like the professor on that video.
I have tried to restart android studio.
I have tried to make new project.
I have tried to clear invalidate caches and restart.
public void changeImage(View view)
{
ImageView bitcoin = findViewById(R.id.bitcoin);
image.setImageResource(R.drawable.xxx);
}
I hope there is actual error with android studio,because code is clone of the video that I am watching.
You are binding your layout's ImageView in Java file with bitcoin variable and you are trying to set an image on an unknown variable 'image'(maybe it's not defined in the class). So you have to set as below.
ImageView bitcoin = findViewById(R.id.bitcoin);
bitcoin.setImageResource(R.drawable.xxx);
Set Your Code Like this
ImageView image = findViewById(R.id.bitcoin);
image.setImageResource(R.drawable.xxx);
change your this line
image.setImageResource(R.drawable.xxx)
to this one:
bitcoin.setImageResource(R.drawable.xxx)

Implementing an image upload button using Jython & Swing

I'm developing a cross platform desktop application using Jython and Swing, and I found a hitch.
I would like to develop a button that allows me to load an image in its background and to change it when I reclick on the image loaded.
I attach as an example some pictures on how I would like my widget to be.
Upload pane without image
And then, when I load an image:
Upload Pane with image
I tried with the following code:
fd = FileDialog(self, "Scegli un'immagine", FileDialog.LOAD)
fd.setFile(';*.'.join(("*.bmp","jpg","jpeg","wbmp","png","gif")))
fd.setVisible(True)
fileName = fd.getFile()
if fileName != None :
fileAbsPath = os.path.abspath(fileName)
"""'self.bigDict['imgButton']' is the current JButton"""
self.bigDict['imgButton'].setIcon(ImageIcon(fileAbsPath))
When I click on "Open", the image is not inserted into the button. I do not understand why.
I also tried with the following code:
if fileName != None :
fileAbsPath = os.path.abspath(fileName)
img = ImageIO.read(getClass().getResource(fileAbsPath))
self.bigDict['imgButton'].setIcon(img)
In the above example the following error is reported:
img = ImageIO.read(getClass().getResource(fileAbsPath))
TypeError: getClass(): expected 1 args; got 0
I would be curious to know why the button is not updated with the image loaded, and why the above error in java would not happen.
Thanks in advance!
getClass() needs 1 argument: the implicit this argument. You must call the method on an object, or use the MyClass.class notation.
The problem is very simple.
When an image is loaded with FileDialog it is "virtually located" in the FileDialog window, but the image does not exist. I realized this when I tried to copy the image from the absolute path to the destination folder, using shutil.copy2(self.imgAbsPath, destinationPath+'/'+self.imgName), which reported an error message indicating that the image does not exist.
To provide the concrete path of a file, it is necessary to add information about the folder where it is located.
In practice you have to create the relative path, before generating the absolute one:
fd = FileDialog(self, "Scegli un'immagine", FileDialog.LOAD)
fd.setFile(';*.'.join(("*.bmp","jpg","jpeg","wbmp","png","gif")))
fd.setVisible(True)
self.imgName = fd.getFile()
relativePath = fd.getDirectory() + fd.getFile() """Here is the missing piece!"""
if self.imgName != None:
self.imgAbsPath = os.path.abspath(relativePath) """Here the absolute path is correctly obtained!"""
self.bigDict['imgButton'].setIcon(ImageIcon(ImageIcon(self.imgAbsPath).getImage().getScaledInstance(130, 130, Image.SCALE_DEFAULT)))
"""The image, before being inserted into the button, is slightly resized, to avoid graphic bumps!"""
I hope I've been helpful.

Android typefont not found

i'm writing this post because I have a problem with android studio on ubuntu mate 17.04.
The problem is that android studio doesn't found a custom font.
I have add this font on:
-main
---assets
------fonts
------------myfont.ttf
but when i do this:
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/FUTRFW.TTF");
classibutton.setTypeface(typeface);
i have an exception that show myfont is not found.
I attach the structure of my project:
how can i solve this??
Thanks all for the help
try this,I think .ttf should not be capital
classibutton = (Button)findViewById(R.id.button1);
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/FUTRFW.ttf");
classibutton.setTypeface(typeface);
try this your have to bind your classibutton than your can set Custom fonts to your and also
don't write .ttf in capital letters
sample code
classibutton=(Button)findViewById(R.id.button1);
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/FUTRFW.ttf");
classibutton.setTypeface(typeface);
Font file name should be in small letters. So First of all you should rename your font file as "futrfw.ttf" and then
classibutton=(Button)findViewById(R.id.button1);
Typeface typeface = Typeface.createFromAsset(getAssets(), "fonts/futrfw.ttf");
classibutton.setTypeface(typeface);
And please be sure that your folder name should be "fonts" (as you are using "fonts/futrfw.ttf" here) and this folder must be inside the "assets" folder which is inside "main" folder.

Custom font button not working Android-Studio

Hello I'm trying to use a Custom font to my Button I've added the font on res/assets.
On my .java I have this code
Button btjoc = (Button) findViewById(R.id.btJoc);
Typeface font = Typeface.createFromAsset(getAssets(), "Heart Breaking Bad.ttf");
btjoc.setTypeface(font);
Button bttaula = (Button) findViewById(R.id.btTaula);
bttaula.setTypeface(font);
And it doesn't work... I don't know if I have to put something else, wish you can help me...
Thanks.
Do you see any logcat error? Do you have related font in your assets directory? try to change your font name in asset folder and your code to Heart_Breaking_Bad.ttf
you must moved it to main/assets instead of res/assets.
and see my answer here , maybe help you.

Issue when using a custom font - "native typeface cannot be made"

I'm trying to use a font I found on the internet, but the problem is that I get an FC with "native typeface cannot be made".
Here is the code in the getView of my ListVIew:
holder.tv_SuraName =(TextView)convertView.findViewById(R.id.Start_Name);
holder.tv_SuraName.setTypeface(Typeface.createFromAsset(mContext.getAssets(), "suralist_font.ttf"));
Can anyone tell me why can I use the custom rom? You can get it HERE .. the file is .ttf
My problem was incorrect placement of the assets folder.
When using Android Studio the assets folder should be inside of the source sets e.g.
src/main/assets/
Found in this answer
The font file is either corrupt or unsupported for some reason. You can drop it on the SD card and load it from file, to make sure it's not a problem with your assets.
There are basically 4 things that can cause this:
You use the wrong extension
You have to place the fonts in the assets folder and not inside assets/fonts/
You misspelled the fonts
The fonts need to be lowercase (in my case the solution was to rename MyFont.ttf to myfont.ttf, strange)
Double check the extension isn't in caps, ie. suralist_font.TTF
Fonts often come seem to come that way and it might be overlooked.
I had this same problem, I created mine in assets>fonts>whatever.ttf and was getting the same error. I added the fonts extension (fonts/whatever.ttf) and it fixed the problem in every case.
holder.tv_SuraName =(TextView)convertView.findViewById(R.id.Start_Name);
holder.tv_SuraName.setTypeface(Typeface.createFromAsset(mContext.getAssets(), "fonts/suralist_font.ttf"));
Rather old thread, but I want to add one more reason this can happen: You have to set the content view before calling typeface. See example below
Does not work:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TextView title = (TextView)findViewById(R.id.tvTitle);
Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/whatever.ttf");
title.setTypeface(font);
setContentView(R.layout.main);
}
Does work!
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // <-- call before typeface
TextView title = (TextView)findViewById(R.id.tvTitle);
Typeface font = Typeface.createFromAsset(this.getAssets(), "fonts/whatever.ttf");
title.setTypeface(font);
}
I am using android studio, in my case problem was with the path of assets folder, in Eclipse path for assets folder is res/assets/ but in Android Studio its src/main/assets/
So, i just move my font file to this path and finally problem solved.
Either your font file is corrupt else you making some mistake in spelling while calling your file name. I had same issue, it was because file name specified in asset folder was
roboto_black.ttf
and while declaring in java file, i have been spelling it as:
roboto_blak.ttf
If your .ttf files are in asset folder,then use:
Typeface type_bold = Typeface.createFromAsset(getAssets(), "roboto_black.ttf");
Else if it is in fonts folder, then use:
type_bold = Typeface.createFromAsset(getAssets(), "fonts/roboto_black.ttf");
Hope it helps!
Hope this will help
Typeface.createFromAsset leaks asset stream: http://code.google.com/p/android/issues/detail?id=9904
As CommonsWare states in this answer
I would guess that there is a problem with the font itself. That error
will be triggered when native code in the OS attempts to load the
typeface.
Anyway try this one:
holder.tv_SuraName = (TextView)convertView.findViewById(R.id.Start_Name);
Typeface Font = Typeface.createFromAsset(this.getAssets(),"suralist_font.ttf");
holder.tv_SuraName.setTypeface(Font);
I had the same problem with COMIC.TTF from comic sans ms of Windows.
Since the original file I placed in assets/fonts folder was in uppercase as spelt above, I had to type it exactly in uppercase.
Typeface.createFromAsset(getAssets(), "fonts/COMIC.TTF");
it turns out the problem was caused by the lower case:
Typeface.createFromAsset(getAssets(), "fonts/comic.ttf");
Please note that this is due to either a corrupt file or you have placed the file in the wrong folder.
According to Android documentation here
main/assets/
This is empty. You can use it to store raw asset files. Files that you save here are compiled into an .apk file as-is, and the original filename is preserved. You can navigate this directory in the same way as a typical file system using URIs and read files as a stream of bytes using the AssetManager. For example, this is a good location for textures and game data.
Therefore your folder structure should be yourmodule/main/assets (e.g. a***pp/main/assets*** )
What worked for me was first changing my font name to only lower case and underscore: from Amatic-Sc.ttf to amatic_sc.ttf.
Then in the set.TypeFace method change assets/fonts/amatic_sc.ttf to just fonts/amatic_sc.ttf.
I've been debugging forever on this error in Android Studio. Clean and rebuilding the project after adding the fonts to the assets directory fixed the issue for me.
Just did a build --> clean and it started rendering .
I had same issue which happened on Android 4.0.3-4.0.4 (100% reproducible on emulator IceCreamSandwich mips 4.0.3).
The problem was that TTF fonts, that I had, were not correct, despite almost all Android versions were able to load those TTF font. So I used https://cloudconvert.com/woff-to-ttf (since I had woff files as well) to make new TTF fonts, which solved my problem.

Categories