I am trying to build my first android app using Kotlin but I am stuck on a very simple situation. I am using Kotlin android extensions and I am getting a Null pointer exception.
<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"/>
With this I used
tvGoToRegisterFrag.setOnClickListener {
goToRegister()
}
I do know I can use
tvGoToRegisterFrag?.setOnClickListener {
goToRegister()
}
To get rid of crashing but still I am not sure why the object is null and does not do anything when clicked. All of this is done in a Fragment if that changes anything.
One reason you may be getting NPE is that you are using it in the wrong lifecycle method I was having a similar error when using it in onCreateView
instead you should only use Kotlin android extensions in onViewCreated
.