Alguém poderia me ajudar? O app recupera os dados da child("grupos")
na depuração, mas quando eu coloco o nó, por exemplo, child("grupos").child("nome")
, o aplicativo da crash e nem na depuração da para ver se está recuperando. O que estou fazendo errado?
Database https://ibb.co/C1Gy8gs
DEBUG https://ibb.co/6b8Whw3
Erro usando child("groupos").child("nome");
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.Iterator java.util.ArrayList.iterator()' on a null object reference
.activity.SalasActivity$1.search(SalasActivity.java:86)
.activity.SalasActivity$1.access$000(SalasActivity.java:56)
.activity.SalasActivity$1$1.onQueryTextChange(SalasActivity.java:78)
public class SalasActivity extends AppCompatActivity {
DatabaseReference ref;
ArrayList<Deal> list;
RecyclerView recyclerView;
SearchView searchView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_salas);
ref = FirebaseDatabase.getInstance().getReference().child("grupos").child("nome");
recyclerView = findViewById(R.id.rv);
searchView = findViewById(R.id.searchView);
}
@Override
protected void onStart() {
super.onStart();
if(ref != null){
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()){
list = new ArrayList<>();
for (DataSnapshot ds : dataSnapshot.getChildren())
{
list.add(ds.getValue(Deal.class));
}
AdapterClass adapterClass = new AdapterClass(list);
recyclerView.setAdapter(adapterClass);
}
if (searchView != null){
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
search(s);
return true;
}
});
}
}
private void search(String str){
ArrayList<Deal> myList = new ArrayList<>();
for (Deal object : list){
if (object.getNome().toLowerCase().contains(str.toLowerCase())){
myList.add(object);
}
}
AdapterClass adapterClass = new AdapterClass(myList);
recyclerView.setAdapter(adapterClass);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(SalasActivity.this,databaseError.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}
}
Deal.class erro E/AndroidRuntime: FATAL EXCEPTION: main Process: xxx.chat, PID: 11966 com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type .activity.Deal
public class Deal {
private String nome;
private String id;
private String membros;
public Deal() {
}
public Deal(String nome, String id, String membros) {
this.nome = nome;
this.id = id;
this.membros = membros;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMembros() {
return membros;
}
public void setMembros(String membros) {
this.membros = membros;
}
}