title: Kotlin: lateinit property has not been initialized when setting TextView
body: <p>I am trying to set the text of a <code>TextView</code> in a fragment. I am calling a method from my Fragment class in my Pager Adapter to accomplish this, but I receive the following error in doing so:</p>
  
  <pre><code>Caused by: kotlin.UninitializedPropertyAccessException: lateinit property totalMsg has not been initialized
  </code></pre>
  
  <p>In other words, it seems as if the <code>onCreateView</code> method is triggered <strong>after</strong> <code>setTotalMessages</code> is called:
  Below is my Kotlin code for the <code>TotalFragment</code> class:</p>
  
  <pre><code>class TotalFragment : Fragment() {
      lateinit var inf:View;
      lateinit var totalMsg:TextView;
  
      override fun onCreateView(
          inflater: LayoutInflater,
          container: ViewGroup?,
          savedInstanceState: Bundle?
      ): View? {
          // Inflate the layout for this fragment
          inf = inflater!!.inflate(R.layout.total_fragment, container, false) as View;
          totalMsg = inf.findViewById(R.id.chat_count)
          Log.i("App", "done")
          return inf;
      }
  
      // Setter method that sets the text of the total messages #
      fun setTotalMessages(messageCount: String) : Unit {
          Log.i("App", "done2")
          totalMsg.text = "total # of messages exchanged: " + messageCount;
          //totalMessages.setText("total # of messages exchanged: " + messageCount);
      }
  }
  </code></pre>
  
  <p>Below is my code for the Pager Adapter:</p>
  
  <pre><code>class PageAdapter(private val myContext: Context, fm: FragmentManager, internal var totalTabs: Int) : FragmentPagerAdapter(fm) {
      // keep track of the fragment containing the cumulative statistics
      var totalFrag:TotalFragment = TotalFragment();
  
      // this is for fragment tabs
      override fun getItem(position: Int): Fragment? {
          when (position) {
              0 -&gt; {
                  return totalFrag
              }
              1 -&gt; {
                  return IndividualFragment()
              }
              2 -&gt; {
                  // val movieFragment = MovieFragment()
                  return UsageFragment()
              }
              else -&gt; return null
          }
      }
  
      // set the text of the cumulative texts label
      fun setCumulativeTexts(messageCount:Int) {
          totalFrag.setTotalMessages(messageCount.toString());
      }
  
      // this counts total number of tabs
      override fun getCount(): Int {
          return totalTabs
      }
  }  
  </code></pre>
  
  <p>Below is my Kotlin code for <code>MainActivity.kt</code> where I call the method to set the cumulative texts:</p>
  
  <pre><code>val adapter = PageAdapter(this, supportFragmentManager, tabLayout!!.tabCount)
  adapter.setCumulativeTexts(5)
  </code></pre>