-1

I want to show the number of active users in my StartActivity. The users are divided into two groups: players and spectators. I tried it in my database with a Boolean value. How can I show / count the users?

Database: Firebase Pic StartActivity: XML Pic

Here is my StartAytivity.class code:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_start);

        Button run = findViewById(R.id.button2);
        run.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(StartActivity.this, MainActivity.class);
                startActivity(intent);
            }
        });

        FirebaseApp.initializeApp(this);

        watcher = findViewById(R.id.textView3);
        player = findViewById(R.id.textView4);

        onlineCount();
    }

Here is my OnlineCount code:

public void onlineCount(){
        //watcher
        refwatcher = FirebaseDatabase.getInstance()
                .getReference("online_list")
                .child("watcher")
                .child(Objects.requireNonNull(FirebaseAuth.getInstance().getCurrentUser()).getUid()).child("online");

        Query query = refwatcher.orderByChild("online").equalTo(true);


        refwatcher.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                wcount = (int) dataSnapshot.getChildrenCount();

                watcher.setText(getString(wcount) + getText(R.string._29_349_watchers_online));


            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });

        //player
        DatabaseReference refplayer = FirebaseDatabase.getInstance().getReference("online_list").child("player");

        refplayer.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                player.setText("1" + getText(R.string._230_player_online));
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });
    }

Here is my debug code:

   Caused by: java.lang.NullPointerException
        at java.util.Objects.requireNonNull(Objects.java:203)
        at com.bbinktattoo.nerve.StartActivity.onlineCount(StartActivity.java:65)
        at com.bbinktattoo.nerve.StartActivity.onCreate(StartActivity.java:57)
        at android.app.Activity.performCreate(Activity.java:7326)
        at android.app.Activity.performCreate(Activity.java:7317)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
  • Please search for the error message before posting a question about it. See https://stackoverflow.com/questions/40081539/default-firebaseapp-is-not-initialized – Frank van Puffelen Jun 16 '19 at 14:49

2 Answers2

1

Your issue has nothing to do with the problem your question implies. You want to know how to count your database entries. Every element in firebase is a node and can have n children and has exactly 1 parent. The relation ship between a node and its parent is 1:1 while the relation ship between the node and its children is 1:n.

So if you want to count the amount of elements you have to call the getChildren() Method on the parent node, which will return n references to the n nodes.

However, it seems like you're not initializing the Firebase SDK as supposed to. Make sure you read and understand the related docs https://firebase.google.com/docs/android/setup

I would guess that you have not set the correct configuration parameters

Martin Nowosad
  • 791
  • 8
  • 15
0

Your issue is not the counting itself but Firebase seems improperly setup in your app. Make sure to follow the official guide step by step. Also check Logcat for errors and warnings from Firebase, usually there is something useful.

crysxd
  • 3,177
  • 20
  • 32