这很奇怪,因为我遇到了一个错误,指出无法将哈希图转换为字符串,但是我没有在该文件中使用任何哈希图,并且我认为哈希图可以很好地用于在数据库中存储值,但是有时产生空异常错误。我对此并不陌生,并且已经阅读了一些相关主题,但是找不到任何解决方案,并且我的logcat中没有显示很多错误...
我认为某些值是null,但不知道如何解决.....我认为我应该使用getter和setter方法,但它们倾向于产生null错误
ublic class ListViewActivity extends AppCompatActivity {
private ListView listview;
private EditText edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
listview = findViewById(R.id.listView);
edit = findViewById(R.id.edit);
final ArrayList<String> list = new ArrayList<>();
final ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.list_item, list);
listview.setAdapter(adapter);
DatabaseReference reference = FirebaseDatabase.getInstance().getReference(); // getReference() is the root
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
list.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String names = String.valueOf(snapshot.child("Name").getValue());
String addresses = String.valueOf(snapshot.child("Address").getValue());
String suburbs = String.valueOf(snapshot.child("suburbs").getValue());
String states = String.valueOf(snapshot.child("State").getValue());
String phones = String.valueOf(snapshot.child("Phone_No").getValue());
String postcodes = String.valueOf(snapshot.child("Postcode").getValue());
String doctors = String.valueOf(snapshot.child("Doctor").getValue());
Information info = snapshot.getValue(Information.class);
assert info != null;
String txt = info.getName() + " , " + info.getAddress() + " ," + info.getSuburb() + " , " + info.getState() + " , " + info.getPhone_No() + " , " + info.getPostcode() + " , " + info.getDoctor();
list.add(txt);
// list.add(snapshot.getValue().toString());
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
}