I am trying to create a system where I can enter a reminder into the Android app, have it sent to Firebase to be accessed to all devices with the app, and have that reminder come up on a notification. I still need to make the interface that I can send things to Firebase from the app, but I am having trouble retrieving the data. Right now it doesn't like my listView.setAdapter(arrayAdapter) line because it thinks I am calling a null object reference. This is my code:
public class MainActivity extends AppCompatActivity {
ListView listView;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
ArrayList<String> arrayList = new ArrayList<>();
ArrayAdapter<String> arrayAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference("Users");
Toast.makeText(this, "ok", Toast.LENGTH_SHORT).show();
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value=dataSnapshot.getValue(String.class);
arrayList.add(value);
arrayAdapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_list_item_1, arrayList);
listView.setAdapter(arrayAdapter);
}//public void onChildAdded(DataSnapshot dataSnapshot, String s) end
class User {
public String username;
public String email;
public User() {
// Default constructor required for calls to DataSnapshot.getValue(User.class)
}
public User(String username, String email) {
this.username = username;
this.email = email;
}
}//class User end
public void writeNewUser(String userId, String name, String email) {
User user = new User(name, email);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}//protected void onCreate(Bundle savedInstanceState) end
}
There are some unused classes, but my main question is, Why is the array null? And why does this crash the app when I try to run it on the emulator?
Also, the only things in my database are parent Users and its one child, "goy" (as a test child).