0

I'm trying to use custom font in my application, but i got that exception when when run the app:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setTypeface(android.graphics.Typeface)' on a null object reference

I have a working font and it's in the right path and that's my code:

private TextView tv;
private Typeface tf;

    tv = (TextView) findViewById(R.id.wlcText);
    tf = Typeface.createFromAsset(getAssets(), "en_font.ttf");
    tv.setTypeface(tf);

That's my XML:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
    android:id="@+id/mainView"
    android:background="@color/semi_transparent_black"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="@dimen/activity_vertical_margin">

    <TextView
        android:id="@+id/wlcText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textColor="@android:color/white"
        android:text="Test"
        android:textSize="25sp" />

</LinearLayout>

Mohamed
  • 656
  • 9
  • 28

1 Answers1

6

tv is null. For whatever reason (e.g., failure to call setContentView()), you do not have a widget in the activity that can be found via R.id.wlcText.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I edited the question and showed the xml, what wrong in it please? – Mohamed May 30 '15 at 17:52
  • 1
    @commons The `tf` is null not `tv`. – oldcode May 30 '15 at 17:55
  • 1
    @Codester `tv` is `null`. He tries to call setTypeface method on a null object. – issathink May 30 '15 at 17:58
  • @issathink By looking in his xml, how can his `tv` be a null ? – oldcode May 30 '15 at 18:00
  • @issathinkSo how to fix that?! am new to Android and don't know actually – Mohamed May 30 '15 at 18:01
  • 2
    He could possibly didn't call `setContentView()` or call it after `findViewById()` – issathink May 30 '15 at 18:02
  • @Mohamed Did you call `setContentView()`, ? – oldcode May 30 '15 at 18:03
  • @Mohamed: Make sure that your code that you have above is being called *after* you inflate this layout, and that your `findViewById()` call matches how you are inflating the layout. As it stands, your Java code shown above would only work if your layout is being inflated via `setContentView()`, and then only if your `findViewById()` call is after the `setContentView()` call. If you are trying to use this layout in other ways (e.g., as the content of a fragment), your code to retrieve the widget would need to change. – CommonsWare May 30 '15 at 18:05
  • @Mohamed: Beyond that, you can try cleaning your project (e.g., Project > Clean from the main menu in Eclipse, Build > Clean Project from the main menu in Android Studio) and see if that helps. Occasionally, the `R` class gets out of sync with the rest of the project. – CommonsWare May 30 '15 at 18:06
  • @CommonsWare it could be more interesting and more visible if you put these explanations in your response. – issathink May 30 '15 at 18:10
  • Yeah i got it, i made custom font for `TextView` in dialog and i set the custom font code in the `onCreate()` bur i must set it in the `dialog` method, so i must do that `tv = (TextView) dialog.findViewById(R.id.wlcText);` now it's works! – Mohamed May 30 '15 at 18:13