title: Changing Toolbar text programmatically from fragment in Kotlin
body: <p>I have an Activity with Fragments as the main content. 
  This Activity has a Toolbar with a TextView inside: </p>
  
  <pre><code>override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(my_activity)
  
      val toolbar = findViewById&lt;Toolbar&gt;(R.id.toolbar)
      setSupportActionBar(toolbar)
      supportActionBar!!.setDisplayHomeAsUpEnabled(false)
      supportActionBar!!.setDisplayShowTitleEnabled(false)
  }
  </code></pre>
  
  <p>Here is the Toolbar in that Activity's xml document:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      tools:context=".MyActivity"&gt;
  
      &lt;include
          android:id="@+id/toolbar"
          layout="@layout/my_toolbar"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"
          app:layout_constraintTop_toTopOf="parent" /&gt;
  
      &lt;FrameLayout
          android:id="@+id/main_content"
          android:layout_width="match_parent"
          android:layout_height="0dp"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintTop_toBottomOf="@id/toolbar"
          app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintStart_toStartOf="parent"&gt;
  
          &lt;fragment android:name="org.siku.siku.MyFragment"
              android:id="@+id/fullscreen_content"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              layout="@layout/my_fragment" /&gt;
  
      &lt;/FrameLayout&gt;
  &lt;/android.support.constraint.ConstraintLayout&gt;
  </code></pre>
  
  <p>And here is the toolbar.xml I am using: </p>
  
  <pre><code>&lt;android.support.v7.widget.Toolbar
      xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"&gt;
  
      &lt;android.support.constraint.ConstraintLayout
          android:layout_width="match_parent"
          android:layout_height="wrap_content"&gt;
  
          &lt;Button
              android:id="@+id/closeBtn"
              android:layout_width="wrap_content"
              android:layout_height="match_parent"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent" /&gt;
  
          &lt;LinearLayout
              android:id="@+id/titleLayout"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent"&gt;
  
              &lt;TextView
                  android:id="@+id/toolBarText1"
                  android:layout_width="wrap_content"
                  android:layout_height="match_parent"/&gt;
          &lt;/LinearLayout&gt;
      &lt;/android.support.constraint.ConstraintLayout&gt;
  &lt;/android.support.v7.widget.Toolbar&gt;
  </code></pre>
  
  <p>I can change the text easily from that Activity, but its impossible for me to change the text from the fragment:</p>
  
  <p>I have tried accessing the TextView from the Fragment, but I am getting an error</p>
  
  <pre><code>override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
      savedInstanceState: Bundle?): View? {
      myView = inflater.inflate(R.layout.my_frament, container, false)
      val toolbar = activity!!.findViewById&lt;Toolbar&gt;(R.id.toolbar)
      val toolbarTextView = toolbar.findViewById&lt;TextView&gt;(R.id.toolBarText1)
  
  }
  </code></pre>
  
  <p>Here is the error I am getting:</p>
  
  <blockquote>
    <p>Caused by: java.lang.NullPointerException: Attempt to invoke virtual
    method 'android.view.View
    android.support.v7.widget.Toolbar.findViewById(int)' on a null object
    reference</p>
  </blockquote>
  
  <p>The error comes from this line: 
      val toolbarTextView = toolbar.findViewById(R.id.toolBarText1)</p>
  
  <p>Which means I am not retrieving that TextView properly, but from the examples I saw, this should be the right way to do so.  </p>