title: How do I load this fragment from Activity in Kotlin?
body: <p>I am attaching my code below.  When I try to run it, the app closes and says there is a bug.  It compiles fine with no errors.  Attaching my files.  I cannot get the debugger to run which is annoying.</p>
  <p>Basically, I want my only button to load the resultsfragment.</p>
  <p><code>MainActivity.kt</code></p>
  <pre><code>package com.example.barcode
  
  import android.content.pm.PackageManager
  import androidx.appcompat.app.AppCompatActivity
  import android.os.Bundle
  import android.util.Log
  import android.view.View
  import android.widget.Button
  import android.widget.Toast
  import androidx.core.app.ActivityCompat
  import androidx.core.content.ContextCompat
  import androidx.fragment.app.Fragment
  import androidx.fragment.app.FragmentTransaction
  import androidx.fragment.app.commit
  import com.budiyev.android.codescanner.AutoFocusMode
  import com.budiyev.android.codescanner.CodeScanner
  import com.budiyev.android.codescanner.DecodeCallback
  import com.budiyev.android.codescanner.ErrorCallback
  import com.budiyev.android.codescanner.ScanMode
  import com.example.barcode.R.layout.fragment_results
  import com.example.barcode.databinding.ActivityMainBinding
  
  
  private const val CAMERA_REQUEST_CODE = 101
  
  class MainActivity : AppCompatActivity() {
  
      private lateinit var binding: ActivityMainBinding
      private lateinit var codeScanner: CodeScanner
  
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          setContentView(R.layout.activity_main)
          binding = ActivityMainBinding.inflate(layoutInflater)
          setContentView(binding.root)
          setupPermissions()
          codeScanner()
  
          var fManager = supportFragmentManager
  
          var tx = fManager.beginTransaction()
          tx.add(R.id.resultsLayout,resultsFragment())
          tx.addToBackStack(null)
          tx.commit()
  
          binding.button.setOnClickListener{
  
              var tx =fManager.beginTransaction()
              tx.replace(R.id.resultsLayout,resultsFragment())
              tx.addToBackStack(null)
              tx.commit()
          }
      }
  
      private fun codeScanner() {
  
          codeScanner = CodeScanner(this, binding.scannerView)
  
          codeScanner.apply {
              codeScanner
              camera = CodeScanner.CAMERA_BACK
              formats = CodeScanner.ALL_FORMATS
  
              autoFocusMode = AutoFocusMode.SAFE
              scanMode = ScanMode.SINGLE
              isAutoFocusEnabled = true
              isFlashEnabled = false
  
              decodeCallback = DecodeCallback {
                  binding.button.text = getString(R.string.searchButtonLabel, it.text)
              }
  
              errorCallback = ErrorCallback {
                  Log.e(&quot;Main&quot;, &quot;Camera Initilization failed ${it.message}&quot;)
              }
          }
          binding.scannerView.setOnClickListener {
              codeScanner.startPreview()
              binding.button.text = getString(R.string.resetButtonLabel)
          }
      }
  
  
      override fun onResume() {
          super.onResume()
          codeScanner.startPreview()
      }
  
      override fun onPause() {
          codeScanner.releaseResources()
          super.onPause()
      }
  
      private fun setupPermissions() {
          val permission = ContextCompat.checkSelfPermission(this, android.Manifest.permission.CAMERA)
          if (permission != PackageManager.PERMISSION_GRANTED) {
              makeRequest()
          }
      }
  
      private fun makeRequest() {
          ActivityCompat.requestPermissions(
              this, arrayOf(android.Manifest.permission.CAMERA),
              CAMERA_REQUEST_CODE
          )
      }
  
      override fun onRequestPermissionsResult(
          requestCode: Int,
          permissions: Array&lt;out String&gt;,
          grantResults: IntArray
      ) {
          when (requestCode) {
              CAMERA_REQUEST_CODE -&gt; {
                  if (grantResults.isEmpty() || grantResults[0] != PackageManager.PERMISSION_GRANTED) {
                      Toast.makeText(this, &quot;You need the camera for this app&quot;, Toast.LENGTH_SHORT)
                          .show()
                  } else {
  
                  }
              }
          }
      }
  }
  </code></pre>
  <p>It runs fine if I comment out this block:</p>
  <pre><code>var fManager = supportFragmentManager
  
  var tx = fManager.beginTransaction()
  tx.add(R.id.resultsLayout,resultsFragment())
  tx.addToBackStack(null)
  tx.commit()
  
  binding.button.setOnClickListener{
  
      var tx =fManager.beginTransaction()
      tx.replace(R.id.resultsLayout,resultsFragment())
      tx.addToBackStack(null)
      tx.commit()
  }
  </code></pre>