I built an app on Android Studio that works perfectly fine, but there´s something that I can´t understand since I´ve tested the app on 6 different android devices, and it works fine with 4 of them, in one of them the app crashes after the registry activity, and in the other one everything works fine until a button is pressed, but it is strange because neither of this errors happens on the other 4 devices.
I can't see any patterns, my only clue is that both of them are related to a process of writing of Shared Preferences I leave the code to you.
I hope you can figure out what I can't see.
mDataBase.child("Users").child("A").addListenerForSingleValueEvent(new ValueEventListener() { // Lectura
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String numcliente = Objects.requireNonNull(dataSnapshot.child("Numero Cliente").getValue()).toString(); // Obtiene valor
Toast.makeText(registro1.this, "Su numero de cliente es: " + numcliente, Toast.LENGTH_SHORT).show();
// Note: This Toast works fine //
SharedPreferences.Editor editor = cliente.edit();
editor.putString("km", km);
editor.putString("numcliente", numcliente);
editor.putString("cel", number);
editor.putString("dir", dir);
editor.putString("ent1", ent1);
editor.putString("ent2", ent2);
editor.putString("name", name);
editor.apply();
final Map <String, Object> map = new HashMap<>();
map.put("Nombre", name);
map.put("Correo Electronico", email);
map.put("Celular", number);
map.put("Contraseña", password);
map.put("Kilometro", km);
map.put("Direccion", dir);
map.put("Entrecalle 1", ent1);
map.put("Entrecalle 2", ent2);
map.put("Referencia", desc);
// map.put("Numero Cliente", numcliente);
//final String id = Objects.requireNonNull(mAuth.getCurrentUser()).getUid();
mDataBase.child("Users").child(numcliente).setValue(map).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task2) {
if(task2.isSuccessful()){
mDataBase.child("Users").child("A").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String numcliente = Objects.requireNonNull(dataSnapshot.child("Numero Cliente").getValue()).toString();
int numcliente1 = Integer.parseInt(numcliente);
final int numcliente2 = numcliente1 + 1;
//final String numclientefinal = Integer.toString(numcliente2);
Map<String, Object> numclientenew = new HashMap<>();
numclientenew.put("Numero Cliente", numcliente2);
mDataBase.child("Users").child("A").setValue(numclientenew).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
//Toast.makeText(registro1.this, "Proximo usuario es " + numcliente2, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
startActivity(new Intent(registro1.this, menu.class));
finish();
}else{
Toast.makeText(registro1.this, "No se pudieron crear los datos", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});