【问题标题】:Dialog Fragment Bundle always null对话框片段包始终为空
【发布时间】:2021-05-25 17:36:57
【问题描述】:

我只想将数据从 Fragment 发送到 DialogFragment。在片段中;

 val fm = requireActivity().supportFragmentManager
            val dialog = DialogFragmentName()
            val arg :Bundle? =null
            arg?.putInt("num",75)
            dialog.arguments = arg
            dialog.show(fm,"TAG")

在 DialogFragment 中;

 val bundle :Bundle? = arguments
    if (bundle != null) {
        val number: Int? = bundle.getInt("num")
        Toast.makeText(context,number,Toast.LENGTH_LONG).show()
    } else {
        Toast.makeText(context,"Null",Toast.LENGTH_LONG).show()
    }

但它总是给我 Null

【问题讨论】:

    标签: android kotlin android-fragments android-dialogfragment dialogfragment


    【解决方案1】:

    您忘记初始化您的Bundle,它被设置为null 并尝试将数据添加到null 引用安全失败,因为?. 运算符最终发送空Bundle

    val fm = requireActivity().supportFragmentManager
    val dialog = DialogFragmentName().apply{
          arguments = Bundle().apply{
              putInt("num",75)
          }
      }
    dialog.show(fm,"TAG")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-21
      • 2013-07-31
      • 2013-12-31
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多