Answer the question
In order to leave comments, you need to log in
How to pass string from Activity to Fragment and pass that string to textview?
I have MainActivity and BottomNavigationDrawerFragment
I need to pass a variable from MainActivity to BottomNavigationDrawerFragment and set the text of the passed variable to texview
Here is my pathetic attempt
MainActivity
override fun showCategory(str: String) {
mCategory!!.text = str
newInstance(str)
}
fun newInstance(index: String): BottomNavigationDrawerFragment {
val f = BottomNavigationDrawerFragment()
// Supply index input as an argument.
val args = Bundle()
args.putString("index", index)
f.arguments = args
return f
}
BottomNavigationDrawerFragment
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
var args = arguments
textViewName.text = args!!.getString("index", "default") // Здесь возникает ошибка
return inflater.inflate(R.layout.fragment_bottom_navigation_drawer, container, false)
}
Answer the question
In order to leave comments, you need to log in
You are accessing textViewName most likely through kotlin-android-extensions, the view does not yet exist in the onCreateView() method. You can either get it from the view that is inflated, or access it after calling onCreateView (), for example, in onActivityCreated ()
val view = inflater.inflate(R.layout.fragment_bottom_navigation_drawer, container, false)
view.textViewName.text = args!!.getString("index", "default")
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
textViewName.text = arguments!!.getString("index", "default")
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question