当应用程序启动时,显示 null。
我认为我没有正确地为 TextView 赋值。
class MyFragment : Fragment() {
private lateinit var textView: TextView
companion object {
private val TEXT_FIELD = "text_field"
@JvmStatic
fun newInstance(text_field: Int) = MyFragment().apply {
arguments = Bundle().apply {
putInt(TEXT_FIELD, text_field)
}
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_my, container, false)
textView = view!!.findViewById(R.id.text_view)
textView.text = "${arguments?.getInt(TEXT_FIELD)}"
return view
}
}
最佳答案
您在赋值中使用了 putInt
,但在尝试检索值时执行了 getString
。
关于android - 我需要将数据从 Activity 输出到 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58399103/