Kotlin setting up font within Fragment

Asked

Viewed 13 times

0

There I am with a doubt, as I do to, modify the source from within a Fragment?

tvAgradecimento.setText("Teste")
        tvAgradecimento.setTypeface(Typeface.createFromAsset(tvAgradecimento.context.assets, "dinpro_medium.ttf"))

ERROR

   java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at br.com.adrianofpinheiro.testesantander.fragment.ContatoEnviadoFragment.onCreateView(ContatoEnviadoFragment.kt:36)

1 answer

0

The exception NullPointerException is trying to say that the TextView is null. This can happen when the ID parameter used when you declared the variable using findViewById is not equal to the ID of the TextView declared in XML. Check that both have the same ID.

If it is not, you said that the text is inside a Fragment, then you may be making the statement of TextView within the Activity, not in the Fragment. When you declare

tvAgradecimento = findViewById<TextView>(R.id.idTextView)

he understands that the TextView belongs to Activity. When you declare

tvAgradecimento = idFragmento.findViewById<TextView>(R.idTextView)

he informs that the TextView belongs to Fragment. Try making this change.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.