mi app se detiene de manera inmediata al presionar el botón de “Registrarse”
Acá mi código:
Registro.java
package com.example.myapplication; import android.app.ProgressDialog; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; import com.google.firebase.auth.AuthResult; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.database.DatabaseReference; import com.google.firebase.database.FirebaseDatabase; import java.util.HashMap; import java.util.Map; public class Registro extends AppCompatActivity{ private EditText et3,et4,et6,et7; private Button btn3; private ProgressDialog progressDialog; //VARIABLES DE DATOS QUE REGISTRAREMOS private String name = ""; private String surname = ""; private String email = ""; private String password =""; FirebaseAuth mAuth; DatabaseReference Database; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_registro); mAuth = FirebaseAuth.getInstance(); //Inicializamos el obj Firebase Database = FirebaseDatabase.getInstance().getReference(); et3 =(EditText)findViewById(R.id.et3); //Igualamos la variable con su respectivo ID et4 =(EditText)findViewById(R.id.et4); et6 =(EditText)findViewById(R.id.et6); et7 =(EditText)findViewById(R.id.et7); btn3 =(Button)findViewById(R.id.btn3); progressDialog = new ProgressDialog(this); btn3.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //Igualamos las variables a los valores que ingreserá el usuario name = et3.getText().toString(); surname = et4.getText().toString(); email = et6.getText().toString(); password = et7.getText().toString(); if(!name.isEmpty() && !surname.isEmpty() && !email.isEmpty() && !password.isEmpty()){ progressDialog.setMessage("Registro en progreso.."); progressDialog.show(); if(password.length()>=6){ RegisterUser(); } else{ Toast.makeText(Registro.this, "El password debe tener al menos 6 caracteres", Toast.LENGTH_SHORT).show(); } } else{ Toast.makeText(Registro.this, "Debe completar los campos", Toast.LENGTH_SHORT).show(); } } }); } private void RegisterUser(){ mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if(task.isSuccessful()){ Map<String, Object> map = new HashMap<>(); //Creamos el objeto mapa y le asignamos el nombre de los campos map.put("name", name); map.put("surname", surname); //con el metodo PUT almacenamos los datos en sus casillas correspondientes map.put("email", email); map.put("password", password); String id = mAuth.getCurrentUser().getUid(); Database.child("Users").child(id).setValue(map).addOnCompleteListener(new OnCompleteListener<Void>() { //Creamos el contenedor principal o hijo donde se almacenaran los valores de email,pass,etc @Override public void onComplete(@NonNull Task<Void> task2) { if(task2.isSuccessful()){ startActivity(new Intent(Registro.this ,Usuario.class)); finish(); } else{ Toast.makeText(Registro.this , "no se pudieron crear los datos correctamente", Toast.LENGTH_SHORT).show(); } } }); } else{ Toast.makeText(Registro.this , "No se pudo registrar el usuario", Toast.LENGTH_SHORT).show(); } progressDialog.dismiss(); } }); }
}
Mi activity principal:
package com.example.myapplication; import android.content.Intent; import android.os.Bundle; import android.view.View; import androidx.appcompat.app.AppCompatActivity; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void Registro (View view){ Intent registro = new Intent(this, Registro.class); startActivity(registro); } }
Mi manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android-permission.INTERNET" /> <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/AppTheme"> <activity android:name=".Usuario"></activity> <activity android:name=".Registro" /> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application>
También tengo todo declarado en el Strings.xml
<resources> <string name="app_name">My Application</string> <string name="tv1">Iniciar Sesión</string> <string name="et1">Email</string> <string name="et2">Password</string> <string name="btn1">INICIAR SESIÓN</string> <string name="btn2">REGISTRARSE</string> <string name="tv2">REGISTRARSE</string> <string name="et3">Nombre</string> <string name="et4">Apellido</string> <string name="et5">Edad</string> <string name="et6">Email</string> <string name="et7">Password</string> <string name="btn3">REGISTRARSE</string> <string name="btn4">INICIO</string> <string name="tv3">Welcome</string> <string name="tv4"></string> </resources>
Como ven, está todo declarado, estoy trabajando con la api17 que es la que me recomendaron.., la app se compila sin errores a excepción por lo que pudieron apreciar en el título, tengo todas las implementaciones necesarias instaladas en el gradle; intenté de varias maneras pero no lo he podido solucionar, esperan me puedan ayudar.. Gracias!