I have a class that extends the AppCompatDialogFragment. I want it to create a popup where the user can input their password. But I get this error every time I run the app.
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
Here is my code for the class onCreateDialog method:
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
//The popup
val mBuilder = AlertDialog.Builder(activity)
val mFlater = activity?.layoutInflater
val mView =mFlater?.inflate(R.layout.activity_get_password, null)
//Get the EditText
val getPassword: EditText = mView!!.findViewById(R.id.getPassword)
mBuilder.setPositiveButton("Ok"){ _, _ ->}
//Set the view
mBuilder.setView(mView)
//Set the AlertDialog
val alertDialog = mBuilder.create().apply {
setCanceledOnTouchOutside(false)
}
//Set the clicklistener for the
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
val password = getPassword.text.toString()
if(password == db?.getPassword()) {
//Send the password
interfaces?.getPassword(password)
//Close the alert dialog
alertDialog.dismiss()
} else //Wrong password?
Toast.makeText(context, "Invalid Password!", Toast.LENGTH_LONG).show()
}
return alertDialog
}