Question
i have a login function in application. It is supposed to check the user input that is given and if it matches the data in
i have a login function in application. It is supposed to check the user input that is given and if it matches the data in the room database it is supposed to direct the user to the next page and if not, the message "login failed" will pop up. Now the problem is when I tap the login button with data in the email and password field the app crashes. Can anyone help me out? Thanks in advance!
This is the error message that am getting:
FATAL EXCEPTION: DefaultDispatcher-worker-1 Process: com.hdtchat, PID: 9856 java.lang.NullPointerException: Can't toast on a thread that has not called Looper.prepare() at com.android.internal.util.Preconditions.checkNotNull(Preconditions.java:167) at android.widget.Toast.getLooper(Toast.java:186) at android.widget.Toast.
This is the login activity:
class Login : AppCompatActivity() { private lateinit var binding: ActivityLoginBinding private lateinit var appDb: Userdatabase private lateinit var button: Button private lateinit var userDao: Dao override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityLoginBinding.inflate(layoutInflater) setContentView(binding.root) appDb = Userdatabase.getDatabase(this) binding.btnSignUp.setOnClickListener { val intent = Intent(this, Signup::class.java) startActivity(intent) } binding.btnLogin.setOnClickListener { readData() } } private fun readData() { val email = binding.edtEmail.text.toString() val password = binding.edtPassword.text.toString() if(email.isNotBlank() && password.isNotBlank()) { lifecycleScope.launch(Dispatchers.IO) { try { val userRepository = UserRepository(userDao) val user = userRepository.verifyUser(email, password) appDb.dao().getUser(edtEmail = email, edtPassword = password) Toast.makeText(this@Login, "Login Success", Toast.LENGTH_SHORT).show() val intent1 = Intent(this@Login, Home::class.java) startActivity(intent1) } catch (e: Exception) { Toast.makeText(this@Login, "Login Failed", Toast. LENGTH_SHORT).show() } } } else { Toast.makeText(this@Login, "Login Failed", Toast.LENGTH_SHORT).show() } } }
This Is the query in DAO fie:
@Query("SELECT email FROM Bruker_Tabell where email LIKE :edtEmail AND password LIKE :edtPassword") fun getUser(edtEmail: String, edtPassword: String): User
This is the UserRepository:
lass UserRepository(private val userDao: Dao) { val readAllData: LiveData> = userDao.readAllData() suspend fun addUser(user: User) { userDao.addUser(user) } fun verifyUser(email: String, password: String): User { try { Log.d("UserRepository", "verifyUser started") val user = userDao.getUser(email, password) Log.d("UserRepository", "getUserByEmail success") if (user.password == password) { return user } else { throw Exception("Invalid Password") } } catch (e: Exception) { Log.e("UserRepository", "verifyUser failed: ${e.message}") throw e } } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started