Android 以编程方式向Kotlin中的活动添加片段

Android 以编程方式向Kotlin中的活动添加片段,android,xml,android-fragments,kotlin,Android,Xml,Android Fragments,Kotlin,我已经建立了一个片段,我希望放在一个活动上 到目前为止,我的代码如下所示: MainActivity.kt class MainActivity: AppCompatActivity() { @Inject lateinit var teamInfoModule: TeamInfoModule; override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstan

我已经建立了一个片段,我希望放在一个活动上

到目前为止,我的代码如下所示:

MainActivity.kt

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<Team> = 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()
    }
}

用容器id替换此行,如下所示

fragmentTransaction.replace(R.id.container, homeFragment).commit()
并在mainactivity xml中添加framelayout

<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container"
    />

您需要为片段提供一个id,并在活动布局中引用它。例如:



将片段添加到活动中

private fun addFragmentToActivity(fragment: Fragment?){

    if (fragment == null) return
    val fm = supportFragmentManager
    val tr = fm.beginTransaction()
    tr.add(R.id.framlayout, fragment)
    tr.commitAllowingStateLoss()
    curFragment = fragment
}
在片段中添加片段

private fun addFragmentToFragment(fragment: Fragment){

 val ft = childFragmentManager.beginTransaction()
    ft.add(R.id.framlayout, fragment, fragment.javaClass.name)
    ft.commitAllowingStateLoss()
}

异常表示您正在尝试添加要与不存在的视图关联的片段。事实上,我不确定
R.id.home\u片段
来自哪里

解决办法是:

<FrameLayout
    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=".MainActivity"
    android:id="@+id/root_container">
</FrameLayout>
因此,另一个答案也是正确的,即使用
container
R.id.container
,但您也缺少
setContentView(R.layout.activity\u main)


嘿,我已经按照你说的做了,但是现在我得到了相同的错误``没有为片段HomeFragment{4b02ab3(6dc9e25c-d62f-4ef7-a264-2ef39a773b5a)的id 0x7f070049(com.example.bluelightlite:id/container)找到视图}```尽量不要保留
curFragment
,除非您还通过当前已知的活动
findFragmentByTag在
onCreate
中对其进行初始化。完美!!!非常感谢,我想这里每个人的答案都是正确的,但是没有人提到setContentView。。。设置为接受应答是否可以使用androidx.fragment.app.FragmentContainerView而不是FrameLayout?如果需要,当然可以
<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/container"
    />
private fun addFragmentToActivity(fragment: Fragment?){

    if (fragment == null) return
    val fm = supportFragmentManager
    val tr = fm.beginTransaction()
    tr.add(R.id.framlayout, fragment)
    tr.commitAllowingStateLoss()
    curFragment = fragment
}
private fun addFragmentToFragment(fragment: Fragment){

 val ft = childFragmentManager.beginTransaction()
    ft.add(R.id.framlayout, fragment, fragment.javaClass.name)
    ft.commitAllowingStateLoss()
}
<FrameLayout
    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=".MainActivity"
    android:id="@+id/root_container">
</FrameLayout>
if(savedInstanceState == null) { // initial transaction should be wrapped like this
    supportFragmentManager.beginTransaction()
           .replace(R.id.root_container, homeFragment)
           .commitAllowingStateLoss()
}
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)