Carlton Jr. Carlton Jr. - 4 months ago 101
Android Question

Changing font in Android making the app to crash?

I want to change the font of a textview inside of android.

Here is the code:

TextView textView = (TextView) findViewById(R.id.textView);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Roboto-Thin.ttf");
textView.setTypeface(font);


And i put the font in
src/main/assets/fonts
folder.

But when I try to run the app (On emulator) it crashes. Why is that?

EDIT: here is my error message I get.

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.asus.wetr, PID: 3973
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.asus.wetr/com.asus.wetr.MainActivity}: java.lang.RuntimeException: Font asset not found fonts/Roboto-Thin.TTF
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.RuntimeException: Font asset not found fonts/Roboto-Thin.TTF
at android.graphics.Typeface.createFromAsset(Typeface.java:190)
at com.asus.wetr.MainActivity.onCreate(MainActivity.java:23)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Application terminated.


EDIT2: here is my activity.xml as requested.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.asus.wetr.MainActivity"
android:background="@drawable/background_color">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="238dp"
android:id="@+id/textView"
android:textAppearance="@style/TextAppearance.AppCompat.Display1"
android:textColor="#FFF"/>
</RelativeLayout>

Answer

I managed to figure my own problem out. Guys, I wanna thank you all you your inputs. I made a small (I'm a novice at this) error, which screwed everything up.

I put this code:

TextView textView = (TextView) findViewById(R.id.textView);
    Typeface thisfont = Typeface.createFromAsset(getAssets(), "Roboto-Thin.ttf");
    textView.setTypeface(thisfont);

before this:

        setContentView(R.layout.activity_main);

which will not work (obvious for most of you why) because the layout have not been set up yet. It has to be after. Lesson learned.

Comments