I have this: <?xml version="1.0" encoding="utf-8"?>…
- User
I have this:
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/drawer_layout" tools:context="com.HidiStudios.BeST.MainActivity">
<RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.HidiStudios.BeST.MainActivity"> <TextView android:id="@+id/leaderboardTitle" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="50dp" android:text="Leaderboard" android:textAlignment="center" android:textSize="24sp" android:textStyle="bold" /> <LinearLayout android:id="@+id/leaderboardLayout" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="@id/leaderboardTitle" android:layout_marginTop="20dp" android:orientation="vertical" android:gravity="center_horizontal"> </LinearLayout> <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto" android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_alignParentBottom="true" ads:adSize="BANNER" ads:adUnitId="@string/BANNER_AD_UNIT_ID"> </com.google.android.gms.ads.AdView> </RelativeLayout> <com.google.android.material.navigation.NavigationView android:layout_width="wrap_content" android:layout_height="match_parent" app:headerLayout="@layout/header" app:menu="@menu/main_menu" android:id="@+id/nav_view" android:layout_gravity="start" /></androidx.drawerlayout.widget.DrawerLayout>
private void leaderboard() { leaderboardQuery.get().addOnCompleteListener(task -> { if (task.isSuccessful()) { LinearLayout leaderboardLayout = findViewById(R.id.leaderboardLayout);
// Clear the layout if you rerun the query to prevent duplicating views leaderboardLayout.removeAllViews(); for (QueryDocumentSnapshot document : task.getResult()) { // Create TextView for each entry TextView scoreView = new TextView(this); String nickname = document.getString("nickname"); String score = document.getLong("score").toString(); String text = nickname != null ? nickname + " " + score : "Anonymous " + score; scoreView.setText(text); scoreView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18); scoreView.setGravity(Gravity.CENTER_HORIZONTAL); leaderboardLayout.addView(scoreView); } } else { Log.w("TagDB", "Error getting documents.", task.getException()); } }); } - Assistant
- User
In my Firebase database, the name of the document is the unique identifier of the user. Can we use this instead of a filed that holds nickname?
- Assistant
- User
MainActivity.java:70: error: non-static variable mAuth cannot be referenced from a static context public static final String currentUserID = mAuth.getUid(); ^
- Assistant
The error you are encountering indicates that you are trying to access an instance variable
mAuthfrom a static context. In Java, static variables and methods belong to the class itself, not instances of the class. Therefore, a static variable or method cannot access non-static (instance) variables or methods, as they require an instance of the class to exist. - User
FATAL EXCEPTION: main Process: com.HidiStudios.BeST, PID: 4787 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseAuth.getUid()' on a null object reference at com.HidiStudios.BeST.LeaderboardActivity.lambda0MethodAndArgsCaller.run(RuntimeInit.java:552) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971)
- Assistant
The
NullPointerExceptionexception that you are encountering is happening because you're trying to callgetUid()on an instance ofFirebaseAuth(mAuth) that has not been initialized (it isnull).To resolve this issue, you need to ensure that
mAuthis initialized before you try to use it. In Android,FirebaseAuthinstances are typically initialized in theonCreate()method of yourActivitysinceonCreate()is called when the activity is being created. - User
- Assistant
- User
In createLeaderboardEntry(document, leaderboardLayout, rank, currentUserId.equals(documentUserId));
Required type: QueryDocumentSnapshot Provided: DocumentSnapshot
- Assistant
- User
Okay, now I have 2 problems: First, if the current user is not in top 10, it does not have any placement displayed. And I need to display the last user with the lowest score on the end of the leaderboard. How can I achieve these?
- Assistant