Dialog Fragment Bundle 始终为空
Dialog Fragment Bundle always null
我只想将数据从 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
您忘记初始化您的 Bundle
,它被设置为 null
并且尝试将数据添加到 null
引用安全失败,因为 ?.
运算符最终发送空 Bundle
val fm = requireActivity().supportFragmentManager
val dialog = DialogFragmentName().apply{
arguments = Bundle().apply{
putInt("num",75)
}
}
dialog.show(fm,"TAG")
我只想将数据从 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
您忘记初始化您的 Bundle
,它被设置为 null
并且尝试将数据添加到 null
引用安全失败,因为 ?.
运算符最终发送空 Bundle
val fm = requireActivity().supportFragmentManager
val dialog = DialogFragmentName().apply{
arguments = Bundle().apply{
putInt("num",75)
}
}
dialog.show(fm,"TAG")