0

我正在尝试将一个片段加载到 Groupie Item() 中,它只是与 java.lang.IllegalStateException 崩溃:活动已被破坏

我用来加载片段的代码适用于常规活动,但不适用于 Groupie 库中 Item() 对象的 bind() 方法。我以为是因为容器需要引用所以我做了一些测试,容器在 bind() 方法中是可以访问的

import android.widget.RadioButton
import android.widget.RadioGroup
import androidx.appcompat.app.AppCompatActivity
import com.xwray.groupie.kotlinandroidextensions.Item
import com.xwray.groupie.kotlinandroidextensions.ViewHolder
import kotlinx.android.synthetic.main.activity_set_frequency.frequency_options
import kotlinx.android.synthetic.main.rx_section_frequency.*


class RxSectionFrequency: Item() {


    companion object comp : AppCompatActivity(){


    }

    lateinit var radioGroup : RadioGroup
    lateinit var radioButton: RadioButton

    override fun bind(viewHolder: ViewHolder, position: Int) {

        fun checkBtn(){
            val frequency_fragment = FrgFrequencyInterval()

            var radioId = radioGroup.checkedRadioButtonId
            radioButton = radioGroup.findViewById(radioId)

            var radio_name = radioButton.text

            var transaction = supportFragmentManager.beginTransaction().add(R.id.fragment_holder,frequency_fragment).commit()
            var removeFragment = supportFragmentManager.beginTransaction().remove(frequency_fragment)


        if(radio_name=="Interval"){ try {transaction} catch (e: Exception){
            println(e)}}

        }

        radioGroup = viewHolder.frequency_options
        radioGroup.setOnCheckedChangeListener{ radioGroup, i -> checkBtn() }

    }

    override fun getLayout()=R.layout.rx_section_frequency
}```


On the radioGroup selection I expect to load a fragment called FrgFrequencyInterval() into a FrameLayout called fragment_holder within rx_section_frequency layout. As the code is throws the following exception and no further explanation as what is causing the error:
java.lang.IllegalStateException: Activity has been destroyed
4

1 回答 1

0

我收到了来自 Groupie 的 Valentin Hinov 的回复,他提到片段不应该使用 RecyclerView 或 LIst 视图,它们当然是 Groupie 库的一部分,而是建议制作自定义视图并使用适配器 getViewTypeCount 和 getView。

于 2019-09-04T13:22:18.683 回答