0

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) {

}
});
bad_coder
  • 11,289
  • 20
  • 44
  • 72

2 Answers2

1

In which device application is crashing, connect is to android studios run an application inside it then simultaneously open logcat in android studios when application gets crashed see in logcat it will show you the reason for crashing the application.

  • Yeah, it´s not a bad idea, but that device is not mine and I cant have it right now, if there´s no other way, i´ll do that when it is possible and I will notify here what´s happening on that device. Thanks man, I will keep you all informed. – Alejo Brignani May 21 '20 at 21:32
0

Ok, I connected the problematic device to my computer, and I did what Vaibhav Kadam told me in his answer. The Logcat gave me a "java.lang.OutOfMemory" trouble, so I came to my beatiful Stack Overflow and I founded this: How to solve java.lang.OutOfMemoryError trouble in Android.

So, I used this simple line android:largeHeap="true" in the AndoridManifest.xml to avoid this memory problem. And it worked on all the problematic devices. It was simpler than i thought.