【问题标题】:adding a child fragment in my bottomsheet fragment in Kotlin在 Kotlin 的底部片段中添加子片段
【发布时间】:2021-08-09 02:31:58
【问题描述】:

我正在尝试在底部工作表中重用已创建的片段,为此我使用以下代码。

class SummaryBottomSheet: BottomSheetDialogFragment() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val bundle = Bundle()
        val diskOnly =false;

        val ft: androidx.fragment.app.FragmentTransaction = childFragmentManager.beginTransaction()
        ft.replace(R.id.reader_placeholder, ReaderFragment())
        ReaderFragment().arguments = Bundle().apply {
            bundle.putBoolean("diskonly",false)
        }
        ft.commit()
    }
}

现在我将捆绑的参数发送到我想在 bootomsheet 片段中拥有的片段。并且该片段还从导航器 navArgs() 接收参数,但这在我的底页中没有发生,因为现在我不是在导航而是在底页中使用相同的片段。

我正在重用的片段是:

lass ReaderFragment : BaseFragment(R.layout.fragment_reader), PageletActionMediator {

    private val args: ReaderFragmentArgs by navArgs()
    private val viewModel: ReaderViewModel by viewModels()
    private val activityViewModel: MainActivityViewModel by activityViewModels()

    private lateinit var recyclerView: RecyclerView
    private lateinit var viewAdapter: PageletListAdapter
    private lateinit var viewManager: LinearLayoutManager

    override fun onCreate(savedInstanceState: Bundle?) {
        Timber.d("created ")


        val arguments = requireArguments()
        val diskOnly = arguments.getBoolean("diskonly")
        shortAnimationDuration = resources.getInteger(android.R.integer.config_shortAnimTime)
 
if(arguments){
        slug?.let { slug ->
            viewModel.initWithSlug(slug, diskOnly)
            return
        }}
else args?.let{
slug ->
            viewModel.initWithSlug(slug, diskOnly)
            return
}
    }

我的应用每次都因错误而崩溃:

java.lang.IllegalStateException: Fragment ReaderFragment{5b3fc12} (7d5de6e0-3258-468c-8db4-d201b4c3f672) id=0x7f090346} does not have any arguments.
                         E      at androidx.fragment.app.Fragment.requireArguments(Fragment.java:679)
                         E      at com.pratilipi.comics.ui.reader.ReaderFragment.onCreate(ReaderFragment.kt:91)
                         E      at androidx.fragment.app.Fragment.performCreate(Fragment.java:2684)
    ```


【问题讨论】:

    标签: android kotlin android-fragments abcustomuinavcontroller


    【解决方案1】:

    您没有在replace 调用中实际使用的片段上设置参数:

    // Here you create a fragment add use it in replace()
    ft.replace(R.id.reader_placeholder, ReaderFragment())
    
    // Here you create a *second* fragment, set its arguments,
    // then do nothing with it.
    ReaderFragment().arguments = Bundle().apply {
        bundle.putBoolean("diskonly",false)
    }
    

    相反,您需要在与replace 一起使用的片段上设置参数:

    val fragment = ReaderFragment()
    fragment.arguments = Bundle().apply {
        bundle.putBoolean("diskonly",false)
    }
    ft.replace(R.id.reader_placeholder, fragment)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-04
      • 2019-03-07
      • 1970-01-01
      • 1970-01-01
      • 2015-07-24
      • 1970-01-01
      相关资源
      最近更新 更多