[英]Android app crashes in some devices for no apparent reason
我在 Android Studio 上構建了一個運行良好的應用程序,但是由於我已經在 6 種不同的 android 設備上測試了該應用程序,所以我無法理解一些東西,並且它適用於其中的 4 個,其中一個是應用程序在注冊表活動后崩潰,而在另一個中一切正常,直到按下按鈕,但這很奇怪,因為其他 4 台設備上都沒有發生此錯誤。
我看不到任何模式,我唯一的線索是它們都與編寫共享首選項的過程有關,我把代碼留給你。
我希望你能弄清楚我看不到的東西。
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) {
}
});
在哪個設備應用程序崩潰時,連接到 android 工作室在其中運行一個應用程序,然后在應用程序崩潰時同時在 android 工作室中打開 logcat,在 logcat 中查看它會顯示應用程序崩潰的原因。
好的,我將有問題的設備連接到我的電腦,我做了Vaibhav Kadam在他的回答中告訴我的事情。 Logcat 給了我一個“java.lang.OutOfMemory”的麻煩,所以我來到了我美麗的 Stack Overflow 並創建了這個: 如何解決 Android 中的 java.lang.OutOfMemoryError 問題。
所以,我在 AndoridManifest.xml 中使用了這個簡單的行android:largeHeap="true"來避免這個memory問題。 它適用於所有有問題的設備。 它比我想象的要簡單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.