Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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.(Toast.java:171) at android.widget.Toast.makeText(Toast.java:498) at android.widget.Toast.makeText(Toast.java:487) at com.hdtchat.Login$readData$1.invokeSuspend(Login.kt:89) at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33) at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:106) at kotlinx.coroutines.internal.LimitedDispatcher.run(LimitedDispatcher.kt:42) at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:95) at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:570) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:750) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:677) at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:664) Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@ab002eb, Dispatchers.IO]

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Harness The Power Of Big Data The IBM Big Data Platform

Authors: Paul Zikopoulos, David Corrigan James Giles Thomas Deutsch Krishnan Parasuraman Dirk DeRoos Paul Zikopoulos

1st Edition

0071808183, 9780071808187

More Books

Students also viewed these Databases questions

Question

In an Excel Pivot Table, how is a Fact/Measure Column repeated?

Answered: 1 week ago