I'm building an app for a personal project but I seem to hit the same error continuously. When the app boots, it will ask the user to enter their account details to log in and as soon as that's done, it also informs that they are signed in with the email used. However, I want the app to show this for 6 seconds and then move to the next activity but for some odd reason, the handler seems to be crashing the app. I've also declared this in the AndroidManifest.xml file as Android Studio suggested but it's still the same issue. Once the timer on Activity_Profile_Dashboard runs out, it should go to the HomeActivity. Thanks. I've attached the error from logcat along with code below:
Activity_Profile_Dashboard.kt:
package com.example.learnr
import android.content.Intent
import android.os.Bundle
import android.os.Handler
import androidx.appcompat.app.ActionBar
import androidx.appcompat.app.AppCompatActivity
import com.example.learnr.databinding.ActivityProfileDashboardBinding
import com.example.learnr.ui.HomeActivity
import com.google.firebase.auth.FirebaseAuth
class activity_profile_dashboard : AppCompatActivity() {
private lateinit var handler: Handler
// ViewBinding
private lateinit var binding: ActivityProfileDashboardBinding
// ActionBar
private lateinit var actionBar: ActionBar
// FirebaseAuth
private lateinit var firebaseAuth: FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityProfileDashboardBinding.inflate(layoutInflater)
setContentView(binding.root)
// configure ActionBar
actionBar = supportActionBar!!
actionBar.title = "Profile"
// init firebase auth
firebaseAuth = FirebaseAuth.getInstance()
checkUser()
// handle click to logout
binding.logoutBtn.setOnClickListener {
firebaseAuth.signOut()
checkUser()
}
}
private fun checkUser() {
// check user is logged in or not
val firebaseUser = firebaseAuth.currentUser
if (firebaseUser != null){
// user not null, user is logged in. Goes to fetch user info
val email = firebaseUser.email
// set it to text view
binding.emailInfo.text = email
handler = Handler()
handler.postDelayed({
val intent = Intent(this, HomeActivity::class.java)
startActivity(intent)
finish()
}, 3000)
/*val handler = Handler()
handler.postDelayed(
{
val intent = Intent(this, HomeActivity::class.java)
startActivity(intent)
finish()
},
6000,
)*/
}
else{
// user is null, user is not logged in. Goes back to login activity
startActivity(Intent(this, MainActivity::class.java))
finish()
}
}
}
HomeActivity.kt:
package com.example.learnr.ui
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import com.example.learnr.R
class HomeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_home)
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.learnr">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Learnr">
<activity
android:name=".HomeActivity"
android:exported="false" />
<activity
android:name=".Main_Dashboard"
android:exported="false"
android:label="@string/title_activity_main_dashboard"
android:theme="@style/Theme.Learnr.NoActionBar" />
<activity
android:name=".activity_profile_dashboard"
android:exported="false" />
<activity
android:name=".activity_register_user"
android:exported="false" />
<activity
android:name=".MainActivity"
android:exported="true"
android:theme="@style/DemoThemeMain" />
<activity
android:name=".SplashScreenActivity"
android:exported="true"
android:theme="@style/DemoTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>