I'm trying to add a click listener to a button inside my fragment using kotlin view binding. I am setting the click listener in the onCreateView method. When I do this I get a null pointer exception since the button is not created yet. I thought the kotlin view binding takes care of the view initialization so the button should not be null?
Here is my code:
class FragmentStart : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_start, container, false)
start_button.setOnClickListener(
Navigation.createNavigateOnClickListener(R.id.action_fragmentStart_to_fragmentQuestion,null)
)
return view
}
}
Here is the exception:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference