I am trying to use a custom font within a TextView of an Android Studio application, but am getting the following error:
It's a null pointer exception; in the below code, txt
is null
for some reason:
Java:
TextView txt;
txt.setText("A");
txt = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Grundschrift.ttf");
txt.setTypeface(font);
XML:
android:id="@+id/custom_font"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="A"
Thanks!
With this part of your,
TextView txt;
txt.setText("A");
implies that you are calling a method setText() in a null object. To use this method, you have to first initialize the TextView.
so change this
TextView txt;
txt.setText("A");
txt = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Grundschrift.ttf");
txt.setTypeface(font);
to
TextView txt;
txt = (TextView) findViewById(R.id.custom_font);
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/Grundschrift.ttf");
txt.setTypeface(font);
txt.setText("A");
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments