android - 使用 Kotlin 的 Android:新活动不显示任何内容,全是空白?
问题描述
我正在尝试在 android 开发中测试自己。为此,我试图在 firebase 的帮助下制作社交媒体应用程序(使用 firebase 身份验证),但问题是。在我使用正确的每个凭据登录后,它没有显示下一个要打开的活动屏幕。我不知道我犯了什么错误。这是 loginAcitivity 屏幕的代码:
class LoginActivity : AppCompatActivity() {
private val firebaseAuth = FirebaseAuth.getInstance()
private val firebaseAuthListener = FirebaseAuth.AuthStateListener {
val user = firebaseAuth.currentUser?.uid
user?.let {
startActivity(HomeActivity.newIntent(this))
finish()
}
}
@SuppressLint("ClickableViewAccessibility")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
setTextChangeListener(emailET, emailTIL)
setTextChangeListener(passwordET, passwordTIL)
loginProgressLayout.setOnTouchListener { v :View, event :MotionEvent -> true }
}
private fun setTextChangeListener(et: EditText, til: TextInputLayout) {
et.addTextChangedListener(object: TextWatcher{
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
}
override fun afterTextChanged(s: Editable?) {
til.isErrorEnabled = false
}
})
}
fun onLogin(v: View){
var proceed = true
if(emailET.text.isNullOrEmpty()){
emailTIL.error = "Email is required"
emailTIL.isErrorEnabled = true
proceed = false
}
if(passwordET.text.isNullOrEmpty()){
passwordTIL.error ="Password is required"
passwordTIL.isErrorEnabled = true
proceed = false
}
if(proceed){
loginProgressLayout.visibility = View.VISIBLE
firebaseAuth.signInWithEmailAndPassword(emailET.text.toString(), passwordET.text.toString())
.addOnCompleteListener { task: Task<AuthResult> ->
if (!task.isSuccessful) {
loginProgressLayout.visibility = View.GONE
Toast.makeText(this@LoginActivity, "Login error: Either the username or password is wrong.", Toast.LENGTH_SHORT).show()
}
}.addOnFailureListener{e: Exception ->
e.printStackTrace()
loginProgressLayout.visibility = View.GONE
}
}
}
fun goToSignUp(v: View){
startActivity(SignUpActivity.newIntent(this))
finish()
}
override fun onStart() {
super.onStart()
firebaseAuth.addAuthStateListener { firebaseAuthListener }
}
override fun onStop() {
super.onStop()
firebaseAuth.removeAuthStateListener { firebaseAuthListener }
}
companion object{
fun newIntent(context: Context) = Intent(context, LoginActivity::class.java)
}
}
为了测试身份验证是否有效,我在活动中放置了一个按钮以注销。
请帮助我,自从我坚持下去已经一周了。
解决方案
您正在使用 lambda 并且在那里您没有执行任何任务。
override fun onStart() {
super.onStart()
firebaseAuth.addAuthStateListener(firebaseAuthListener)
}
override fun onStop() {
super.onStop()
firebaseAuth.removeAuthStateListener(firebaseAuthListener)
}
推荐阅读
- amazon-web-services - 删除后未重新创建 AWS Lambda 日志组
- node.js - 如何使用 Passport.js 创建管理员帐户
- javascript - Dymo JavaScript API 可以连接到托管在同一网络的不同机器上的 Dymo Web 服务吗?
- python - Python 3.7 中有没有办法将字符串方程转换为像 Wolfram Alpha 这样的公式化方程?
- ruby-on-rails - NoMethodError:#User:0x000055ce01dcf0a8 的未定义方法 `current_sign_in_at' 使用 Devise_token_auth rails gem 不起作用
- azure-resource-group - 区域对于资源组是否重要?
- jupyter-notebook - 如何在 Jupyter Notebook 小部件中使用 FabricJS
- python - Selenium select_by_value 然后打印选项文本
- kotlin - 为什么每个线程多次初始化惰性变量
- windows - 如何在powershell中使用short_command_name -args而不是python program_name -args?