stackoverflow! I'm new to developing in android and I'm trying to figure out Fragments in android. I created a FrameLayout in activity_main.xml
<FrameLayout
android:id="@+id/mainFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
And in onCreate method of MainActivity I'm trying to pass the LoginFragment to the FrameLayout. Here's the code of MainActivity:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
showFragment(LoginFragment())
passwordInput.addTextChangedListener(
object : TextWatcher {
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
override fun afterTextChanged(p0: Editable?) {
logInButton.isEnabled =
passwordInput.text.isNotBlank() && loginInput.text.isNotBlank()
}
})
loginInput.addTextChangedListener(
object : TextWatcher {
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
override fun afterTextChanged(p0: Editable?) {
logInButton.isEnabled =
passwordInput.text.isNotBlank() && loginInput.text.isNotBlank()
}
})
logInButton.setOnClickListener {
showFragment(LoginFragment())
}
}
private fun showFragment(fragment: Fragment) {
supportFragmentManager.beginTransaction()
.replace(R.id.mainFragment, fragment)
.commit()
}}
The LoginFragment code:
class LoginFragment : Fragment(R.layout.fragment_login) {}
And the fragment_login.xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:id="@+id/loginInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="80dp"
android:layout_marginEnd="40dp"
android:autofillHints="username"
android:hint="@string/enter_login"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/passwordInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="40dp"
android:layout_marginBottom="20dp"
android:autofillHints="password"
android:hint="@string/enter_password"
android:inputType="textPassword" />
<Button
android:id="@+id/logInButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_marginEnd="40dp"
android:enabled="false"
android:text="@string/log_in" />
</LinearLayout>
I got the null pointer:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.addTextChangedListener(android.text.TextWatcher)' on a null object reference at com.example.fragmentactivity.MainActivity.onCreate(MainActivity.kt:18)
I can't really understand what's wrong. It says that I'm trying to refer an empty veiw but all thoose views are viewBinded and in IDE they are shown as they should be. Thanks in advance for all the hints and answers