首页 > 解决方案 > How can I change the fonts in android using Kotlin?

问题描述

I am try change the font, but not working. Are returning "null" being that I set the value in the XML and also in the code.

Kotlin Code

 val typeFace = Typeface.createFromAsset(tvThanks.context.assets, "dinpro_medium.ttf")
        tvThanks.setTypeface(typeFace)
<TextView
    android:id="@+id/tvThanks"
    android:layout_width="150dp"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:text="@string/text_here"/>

ERROR

java.lang.IllegalStateException: tvThanks must not be null
    at br.com.adrianofpinheiro.testesantander.fragment.ContatoEnviadoFragment.onCreate(ContatoEnviadoFragment.kt:21)

标签: androidkotlinfontstextview

解决方案


Your fragment should have the context property without getting from the tvThanks.

I think you can try doing this inside the onCreateView of your fragment

 val typeFace = Typeface.createFromAsset(context.assets, "dinpro_medium.ttf")
        tvThanks!!.setTypeface(typeFace)

推荐阅读