[英]Error while using Firebase Android Studio (kotlin)
I'm trying to make a project with Firebase login and signup, I'm trying now to make the signup activity, I attach the code here:我正在尝试使用 Firebase 登录和注册创建一个项目,我现在正在尝试进行注册活动,我在此处附上代码:
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.view.View
import android.widget.EditText
import android.widget.ProgressBar
import android.widget.Toast
import com.google.firebase.auth.FirebaseAuth
import com.google.firebase.auth.FirebaseUser
import com.google.firebase.database.DatabaseReference
import com.google.firebase.database.FirebaseDatabase
class RegisterActivity : AppCompatActivity() {
private lateinit var txtName:EditText
private lateinit var txtLastName:EditText
private lateinit var txtEmail:EditText
private lateinit var txtPassword:EditText
private lateinit var progressBar:ProgressBar
private lateinit var dbReference:DatabaseReference
private lateinit var database:FirebaseDatabase
private lateinit var auth:FirebaseAuth
override fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_register)
txtName=findViewById(R.id.txtName)
txtLastName=findViewById(R.id.txtLastName)
txtEmail=findViewById(R.id.txtEmail)
txtPassword=findViewById(R.id.txtPassword)
progressBar=findViewById(R.id.progressBar)
database= FirebaseDatabase.getInstance()
auth= FirebaseAuth.getInstance()
dbReference=database.reference.child("Usuarios")
}
fun registrarse(view:View){
createNewAccount()
}
fun login(view:View) {
startActivity(Intent(this,LoginActivity::class.java))
}
private fun createNewAccount(){
val name:String=txtName.text.toString()
val lastName:String=txtLastName.text.toString()
val email:String=txtEmail.text.toString()
val password:String=txtPassword.text.toString()
if(!TextUtils.isEmpty(name) && !TextUtils.isEmpty(lastName) && !TextUtils.isEmpty(email) && !TextUtils.isEmpty(password)){
progressBar.visibility=View.VISIBLE
auth.createUserWithEmailAndPassword(email,password)
?.addOnCompleteListener(this){
task ->
if(task.isComplete){
val user:FirebaseUser?=auth.currentUser
verifyEmail(user)
val userBD=dbReference.child(user?.uid)
userBD.child("Nombre").setValue(name)
userBD.child("Apellidos").setValue(lastName)
userBD.child("Correo electrónico").setValue(email)
action()
}
}
}
}
private fun verifyEmail(user:FirebaseUser?){
user?.sendEmailVerification()
?.addOnCompleteListener(this){
task ->
if(task.isComplete){
Toast.makeText(this, "Correo de verificación enviado.",Toast.LENGTH_LONG).show()
}
else{
Toast.makeText(this, "Error al enviar el correo de verificación.",Toast.LENGTH_LONG).show()
}
}
}
private fun action(){
startActivity(Intent(this,LoginActivity::class.java))
}
}
When I press button "registrarse", app crashes... My gradle file (App) is the following:当我按下“registrarse”按钮时,应用程序崩溃了……我的 gradle 文件(应用程序)如下:
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.example.uburr.hastalos"
minSdkVersion 24
targetSdkVersion 26
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jre7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
implementation 'com.google.firebase:firebase-core:12.0.1'
implementation 'com.google.firebase:firebase-database:12.0.1'
implementation 'com.google.firebase:firebase-auth:12.0.1'
implementation 'com.google.firebase:firebase-storage:12.0.1'
implementation 'com.google.firebase:firebase-messaging:12.0.1'
}
apply plugin: 'com.google.gms.google-services'
Compiler doesn't show any error.编译器没有显示任何错误。 I'm new to this, so I really don't know how to fix this... I'm trying to learn, I think this forum will help me, I'll be very grateful to everyone that try to help.
我是新手,所以我真的不知道如何解决这个问题......我正在努力学习,我认为这个论坛会对我有所帮助,我将非常感谢所有试图提供帮助的人。
03-30 17:23:03.636 12627-12627/com.example.uburr.hastalos E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.uburr.hastalos, PID: 12627 java.lang.NullPointerException: Can't pass null for argument 'pathString' in child() at com.google.firebase.database.DatabaseReference.child(Unknown Source) at com.example.uburr.hastalos.RegisterActivity$createNewAccount$1.onComplete(RegisterActivity.kt:69) at com.google.android.gms.tasks.zzf.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at 88168970
03-30 17:23:03.636 12627-12627/com.example.uburr.hastalos E/AndroidRuntime:致命异常:主进程:com.example.uburr.hastalos,PID:12627 java.lang.NullPoint passer null for argument 'pathString' in child() at com.google.firebase.database.DatabaseReference.child(Unknown Source) at com.example.uburr.hastalos.RegisterActivity$createNewAccount$1.onComplete(RegisterActivity.kt:69) at com .google.android.gms.tasks.zzf.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os .Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller .run(ZygoteInit.java:886) 在 88168970 007088.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 03-30 17:23:14.789 12627-12733/com.example.uburr.hastalos I/Adreno: DequeueBuffer: dequeueBuffer failed 03-30 17:23:14.789 12627-12733/com.example.uburr.hastalos I/Adreno: DequeueBuffer: dequeueBuffer failed 03-30 17:23:14.793 12627-12733/com.example.uburr.hastalos W/OpenGLRenderer: swapBuffers encountered EGL error 12301 on 0x7f6b9f2380, halting rendering...
007088.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 03-30 17:23:14.789 12627-12733/com.example.uburr.hastalos I/Adreno: DequeueBuffer0:dequeueBuffer3 失败14.789 12627-12733/com.example.uburr.hastalos I/Adreno:DequeueBuffer:dequeueBuffer 失败 03-30 17:23:14.793 12627-12733/com.example.uburr.hastalos W/OpenGLRenderer:swapBuffers 在 0f27380b9 上遇到 EGL 错误 12301 , 停止渲染...
Thanks a lot, Miguel Angel.非常感谢,米格尔天使。
The problem is here, you're checking if the task is complete and then try to access the user, but if the task is not successfull the user will be null: 问题出在这里,您正在检查任务是否完成,然后尝试访问用户,但是如果任务未成功,则用户将为空:
if(task.isComplete){
val user:FirebaseUser?=auth.currentUser
verifyEmail(user)
val userBD=dbReference.child(user?.uid)
userBD.child("Nombre").setValue(name)
userBD.child("Apellidos").setValue(lastName)
userBD.child("Correo electrónico").setValue(email)
action()
}
You should do instead, check if the task is succesfull and then act on it: 您应该改而做,检查任务是否成功,然后对其执行操作:
if(task.isSuccessful){
val user:FirebaseUser?=auth.currentUser
verifyEmail(user)
val userBD=dbReference.child(user?.uid)
userBD.child("Nombre").setValue(name)
userBD.child("Apellidos").setValue(lastName)
userBD.child("Correo electrónico").setValue(email)
action()
}
You need to add in your gradle (:app) > dependencies您需要添加您的 gradle (:app) > dependencies
implementation 'com.google.firebase:firebase-auth-ktx'实施 'com.google.firebase:firebase-auth-ktx'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.