点击一个文本视图来改变片段

我正在尝试使用Kotlin构建我的第一个Android应用程序,但是我陷入了一个非常简单的情况。 我正在使用Kotlin android扩展,我得到一个空指针异常。

<TextView android:id="@+id/tvGoToRegisterFrag" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/btn_sign_in" android:text="@string/haven_t_registered_yet" android:paddingTop="10dp" android:textColor="@color/colorAccent" android:layout_centerHorizontal="true"/> 

用这个我用了

 tvGoToRegisterFrag.setOnClickListener { goToRegister() } 

我知道我可以使用

 tvGoToRegisterFrag?.setOnClickListener { goToRegister() } 

为了摆脱崩溃,但我仍然不知道为什么对象是空的,点击时不做任何事情。 所有这些都是在片段中完成的,如果这改变了任何东西。

你可能得到NPE的一个原因是你在错误的生命周期方法中使用它在onCreateView使用它时,我有一个类似的错误,而不应该使用onViewCreated Kotlin android扩展。