title: Change TextView in MainActvity from RecyclerAdapter using Kotlin
body: <p>I want to change the text in a TextView (id: dailyTotalTextView) in MainActivity, when I click a button in a RecyclerView. But I get an error saying dailyTotalTextView must not be null.</p>
  
  <p>I have a basic knowledge of Kotlin and no Java experience, so I might be going all wrong about this. </p>
  
  <p>The TextView displays the sum of values in a list. The list items contain a number and a button to delete the item. When I delete an item I want the TextView to change the total.</p>
  
  <p>In MainActivity an item can be added to the list and by using .notifyDataSetChanged() I can set the text to the new value. </p>
  
  <p>The DrinksToday object holds the variables and methods for changing the list. </p>
  
  <p>The adapter holds the onClick for the delete button, so it can delete the item at the clicked position. This is also where I wanted to change the TextView.</p>
  
  <p>MainActivity:</p>
  
  <pre><code>class MainActivity : AppCompatActivity() {
  
      private lateinit var adapter: TodayDrinksRecyclerAdapter
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
  
          mainTextDailyTotal.text = dailyTotal.toString()
  
          adapter = TodayDrinksRecyclerAdapter(this, DrinksToday.drinksTodayList)
          drinksTodayList.adapter = adapter
  
          val layoutManager = LinearLayoutManager(this)
          drinksTodayList?.layoutManager = layoutManager
      }
  
      fun addWaterClick(@Suppress("UNUSED_PARAMETER") view: View) {
          DrinksToday.addDrink()
          adapter.notifyDataSetChanged()
          mainTextDailyTotal.text = dailyTotal.toString()
      }
  }
  </code></pre>
  
  <p>DrinksToday object:</p>
  
  <pre><code>object DrinksToday {
  
      var currentGlass = Drinks("water01", "250", "ml")
      var dailyTotal = 0
      var drinksTodayList: MutableList&lt;Drinks&gt; = mutableListOf()
  
      fun addDrink() {
          dailyTotal += currentGlass.volume.toInt()
          drinksTodayList.add(0, currentGlass)
      }
  
      fun removeDrink(position: Int) {
          drinksTodayList.removeAt(position)
      }
  }
  </code></pre>
  
  <p><strong>Edit:</strong> I changed the inner class Holder and now use kotlinx.android.synthetic to access the view references.</p>
  
  <p>RecyclerAdapter:</p>
  
  <pre><code>class TodayDrinksRecyclerAdapter(private val context: Context, private val todaysDrinks: MutableList&lt;Drinks&gt;) :
      RecyclerView.Adapter&lt;TodayDrinksRecyclerAdapter.Holder&gt;() {
  
  // ... standard adapter code here: onBindViewHolder, getItemCount, onCreateViewHolder
  
      open inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
          fun bindDrinks(drinks: Drinks, context: Context) {
              itemView.drinkListText.text = drinks.volume
              itemView.drinkListButtonDelete.setOnClickListener {
                  val position = adapterPosition
                  DrinksToday.removeDrink(position)
                  DrinksToday.dailyTotal -= drinks.volume.toInt()
                  notifyDataSetChanged()
  
                  val dailyTotalTextView = itemView.findViewById&lt;TextView&gt;(R.id.mainTextDailyTotal)
                  dailyTotalTextView.text = DrinksToday.dailyTotal.toString()
              }
          }
      }
  }
  </code></pre>
  
  <p>TextView XML:</p>
  
  <pre><code>&lt;TextView
              android:id="@+id/mainTextDailyTotal"
              android:text="0"
              android:layout_width="0dp"
              android:layout_height="0dp"/&gt;
  </code></pre>
  
  <p>When I run this code I get an error saying:</p>
  
  <pre><code>java.lang.IllegalStateException: dailyTotalTextView must not be null at adapters.TodayDrinksRecyclerAdapter$Holder$bindDrinks$1.onClick(TodayDrinksRecyclerAdapter.kt:54)
  </code></pre>
  
  <p>Which refers to is this line of code:</p>
  
  <pre><code>val dailyTotalTextView = itemView.findViewById&lt;TextView&gt;(R.id.mainTextDailyTotal)
  </code></pre>
  
  <p>I don't understand why this TextView is Null. I would expect this to work. Hopefully someone can help me out.</p>