title: Programmatically add Fragment to an Activity in Kotlin
body: <p>I have built a fragment that I wish to place on an activity. </p>
  
  <p>So far, my code looks like this: </p>
  
  <p>MainActivity.kt</p>
  
  <pre><code>class MainActivity: AppCompatActivity() {
  
      @Inject
      lateinit var teamInfoModule: TeamInfoModule;
  
      override fun onCreate(savedInstanceState: Bundle?) {
          super.onCreate(savedInstanceState)
          DaggerServiceModuleComponent.create().inject(this)
  
          val bundle = Bundle()
          val teamArrayList: ArrayList&lt;Team&gt; = this.teamInfoModule.getAllTeamData()
          val homeFragment = HomeFragment()
  
          bundle.putParcelableArrayList("teamData", teamArrayList)
          homeFragment.arguments = bundle
  
          val fragmentManager: FragmentManager = supportFragmentManager
          val fragmentTransaction: FragmentTransaction = fragmentManager.beginTransaction()
          fragmentTransaction.replace(R.id.home_fragment, homeFragment).commit()
      }
  }
  </code></pre>
  
  <p>activity_main.xml</p>
  
  <pre><code>&lt;layout 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"&gt;
  
      &lt;androidx.constraintlayout.widget.ConstraintLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context=".MainActivity"
          android:id="@+id/activity_main"&gt;
      &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  &lt;/layout&gt;
  </code></pre>
  
  <p>HomeFragment.kt</p>
  
  <pre><code>class HomeFragment @Inject constructor(): Fragment() {
  
      lateinit var team: Team
  
      override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
          super.onCreateView(inflater, container, savedInstanceState)
  
          DaggerServiceModuleComponent.create().inject(this)
  
          val binding: ViewDataBinding = DataBindingUtil.inflate&lt;ViewDataBinding&gt;(inflater, R.layout.layout_home, container, false)
          print(arguments.toString())
          val allTeams: ArrayList&lt;Team&gt; = arguments?.get("teamData") as ArrayList&lt;Team&gt;
  
          this.team = allTeams[0]
  
          binding.setVariable(BR.team, team)
  
          return binding.root
      }
  }
  </code></pre>
  
  <p>layout_home.xml (tied to HomeFragment)</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;layout 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"&gt;
  
      &lt;data&gt;
          &lt;variable
              name="team"
              type="com.example.bluelightlite.models.Team" /&gt;
      &lt;/data&gt;
  
      &lt;androidx.constraintlayout.widget.ConstraintLayout
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          tools:context=".fragments.HomeFragment"&gt;
  
          &lt;androidx.cardview.widget.CardView
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:layout_marginStart="1dp"
              android:layout_marginTop="79dp"
              android:layout_marginEnd="1dp"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintHorizontal_bias="0.0"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent"&gt;
  
              &lt;TextView
                  android:id="@+id/info_text"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:text="@{team.name}"/&gt;
          &lt;/androidx.cardview.widget.CardView&gt;
  
      &lt;/androidx.constraintlayout.widget.ConstraintLayout&gt;
  &lt;/layout&gt;
  </code></pre>
  
  <p>When it runs, I get the error: </p>
  
  <pre><code>E/AndroidRuntime: FATAL EXCEPTION: main
      Process: com.example.bluelightlite, PID: 10421
      java.lang.IllegalArgumentException: No view found for id 0x7f07003a (com.example.bluelightlite:id/activity_main) for fragment HomeFragment{4b02ab3 (55c458bd-2931-48c6-8087-5e82641313c1) id=0x7f07003a}
  </code></pre>