使用Firebase Android Studio(kotlin)时出错

时间:2018-03-29 20:58:03

标签: android firebase firebase-realtime-database kotlin firebase-authentication

我尝试使用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))
    }

}

当我按下按钮"注册"时,应用程序崩溃...我的gradle文件(App)如下:

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'

编译器没有显示任何错误。我是新手,所以我真的不知道如何解决这个问题......我想学习,我认为这个论坛会对我有所帮助,我会非常感激对每个试图帮助的人。

  

03-30 17:23:03.636 12627-12627 / com.example.uburr.hastalos   E / AndroidRuntime:致命异常:主要       处理:com.example.uburr.hastalos,PID:12627       java.lang.NullPointerException:不能为参数' pathString'传递null。在孩子()           在com.google.firebase.database.DatabaseReference.child(未知来源)           at com.example.uburr.hastalos.RegisterActivity $ createNewAccount $ 1.onComplete(RegisterActivity.kt:69)           在com.google.android.gms.tasks.zzf.run(未知来源)           在android.os.Handler.handleCallback(Handler.java:751)           在android.os.Handler.dispatchMessage(Handler.java:95)           在android.os.Looper.loop(Looper.java:154)           在android.app.ActivityThread.main(ActivityThread.java:6119)           at java.lang.reflect.Method.invoke(Native Method)           在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)03-30   17:23:14.789 12627-12733 / com.example.uburr.hastalos I / Adreno:   DequeueBuffer:dequeueBuffer 03-30 17:23: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   在0x7f6b9f2380上遇到EGL错误12301,停止渲染...

非常感谢, 米格尔·安吉尔。

1 个答案:

答案 0 :(得分:0)

问题出在这里,你要检查任务是否完成然后尝试访问用户,但如果任务不成功,则用户将为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()
                        }

您应该这样做,检查任务是否成功,然后对其采取行动:

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()
                        }