Can't write in Firebase Realtime Database from Android

1

I'm trying to make a simple Android App with Firebase.

At the moment I'm just trying to save users in real time Database from Firebase. But for some reason it doesn't work. On the other hand, the authentication works perfectly.

The Database itself doesnt show anything. It allways shows "null"

I've been following this YouTube tutorial and have been making things pretty similar.

When i try to create one user and send it to the Database, I have no answer from the Running program. I have a progress bar that goes invisible as soon as the setValue(user) is completed. And that progressBar never goes invisible. Just stays there spinning.

Activity

private ProgressBar progressBar;
FirebaseAuth auth = FirebaseAuth.getInstance();

FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("users");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_register);

    progressBar = findViewById(R.id.progressBar);
    progressBar.setVisibility(View.INVISIBLE);

    registerButton = findViewById(R.id.registerButton);
    registerButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            registerUser();
        }
    });
}

private void registerUser() {
    String name = nameTV.getText().toString().trim();
    String email = emailTV.getText().toString().trim();
    String pwd = pwdTV.getText().toString().trim();
    String confirmPwd = confirmPwdTV.getText().toString().trim();

    progressBar.setVisibility(View.VISIBLE);

    auth.createUserWithEmailAndPassword(email, pwd)
            .addOnCompleteListener(ActivityRegister.this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        User user = new User(email, name, pwd);

                        myRef.setValue(user).addOnCompleteListener(new OnCompleteListener<Void>() {
                            @Override
                            public void onComplete(@NonNull Task<Void> task) {
                                if (task.isSuccessful()) {
                                    Toast.makeText(ActivityRegister.this, "User has been registered successfully", Toast.LENGTH_LONG).show();
                                    progressBar.setVisibility(View.GONE);

                                    startActivity(new Intent(ActivityRegister.this, ActivityLogin.class));
                                } else {
                                    Toast.makeText(ActivityRegister.this, "Failed to register. Try again", Toast.LENGTH_LONG).show();
                                    progressBar.setVisibility(View.GONE);
                                }
                            }
                        });
                    } else {
                        Toast.makeText(ActivityRegister.this, "Failed to register. Try again", Toast.LENGTH_LONG).show();
                        progressBar.setVisibility(View.GONE);
                    }
                }
            });

}

Dependencies

implementation platform('com.google.firebase:firebase-bom:26.2.0')
implementation 'com.google.firebase:firebase-analytics'
implementation 'com.google.firebase:firebase-database:19.6.0'

This is the stack trace too, in case it helps

$ adb shell am start -n "com.example.ondestudo/com.example.ondestudo.Views.ActivityLogin" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Connected to process 22029 on device 'google-pixel_4a-0A061JEC206185'.
Capturing and displaying logcat messages from application. This behavior can be disabled in the "Logcat output" section of the "Debugger" settings page.
D/ApplicationLoaders: Returning zygote-cached class loader: /system/framework/android.test.base.jar
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
D/NetworkSecurityConfig: No Network Security Config specified, using platform default
W/ComponentDiscovery: Class com.google.firebase.dynamicloading.DynamicLoadingRegistrar is not an found.
I/FirebaseApp: Device unlocked: initializing all Firebase APIs for app [DEFAULT]
D/FirebaseAuth: Notifying id token listeners about user ( 9KzxeL5yGvawhLkXxCks9yPKVyO2 ).
I/FirebaseInitProvider: FirebaseApp initialization successful
I/FirebaseAuth: [FirebaseAuth:] Preparing to create service connection to fallback implementation
I/DynamiteModule: Considering local module com.google.android.gms.measurement.dynamite:20 and remote module com.google.android.gms.measurement.dynamite:21
    Selected remote version of com.google.android.gms.measurement.dynamite, version >= 21
V/DynamiteModule: Dynamite loader version >= 2, using loadModule2NoCrashUtils
W/ample.ondestud: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (greylist, reflection, allowed)
    Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (greylist, reflection, allowed)
I/DynamiteLoaderV2Impl: [71] Measurementdynamite
V/FA: onActivityCreated
I/AdrenoGLES-0: QUALCOMM build                   : 0905e9f, Ia11ce2d146
    Build Date                       : 09/02/20
    OpenGL ES Shader Compiler Version: EV031.31.04.00
    Local Branch                     : gfx-adreno.lnx.2.0
    Remote Branch                    : 
    Remote Branch                    : 
    Reconstruct Branch               : 
    Build Config                     : S P 10.0.4 AArch64
    Driver Path                      : /vendor/lib64/egl/libGLESv2_adreno.so
I/AdrenoGLES-0: PFP: 0x016ee189, ME: 0x00000000
W/AdrenoUtils: <ReadGpuID_from_sysfs:197>: Failed to open /sys/class/kgsl/kgsl-3d0/gpu_model
    <ReadGpuID:221>: Failed to read chip ID from gpu_model. Fallback to use the GSL path
I/Gralloc4: mapper 4.x is not supported
V/FA: App measurement collection enabled
V/FA: App measurement enabled for app package, google app id: com.example.ondestudo, 1:1004089938689:android:1a5c4a963fa9f00c588100
I/FA: App measurement initialized, version: 34027
    To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
I/FA: To enable faster debug mode event logging run:
      adb shell setprop debug.firebase.analytics.app com.example.ondestudo
D/FA: Debug-level message logging enabled
V/FA: Connecting to remote service
V/FA: Connection attempt already in progress
V/FA: Connection attempt already in progress
V/FA: Activity resumed, time: 237947682
I/FA: Tag Manager is not found and thus will not be used
V/FA: Connection attempt already in progress
V/FA: Connection attempt already in progress
D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 5
V/FA: Recording user engagement, ms: 3061
V/FA: Activity paused, time: 237950744
V/FA: onActivityCreated
V/FA: Activity resumed, time: 237950824
I/AssistStructure: Flattened final assist data: 2280 bytes, containing 1 windows, 12 views
V/FA: Recording user engagement, ms: 3001
V/FA: Activity paused, time: 237953826
V/FA: Activity resumed, time: 237953905
V/FA: Inactivity, disconnecting from the service
W/ConnectionTracker: Exception thrown while unbinding
    java.lang.IllegalArgumentException: Service not registered: ls@ef40aaa
        at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:1781)
        at android.app.ContextImpl.unbindService(ContextImpl.java:1874)
        at android.content.ContextWrapper.unbindService(ContextWrapper.java:792)
        at ci.f(:com.google.android.gms.dynamite_measurementdynamite@204714097@20.47.14 (150400-0):1)
        at ci.d(:com.google.android.gms.dynamite_measurementdynamite@204714097@20.47.14 (150400-0):2)
        at lt.E(:com.google.android.gms.dynamite_measurementdynamite@204714097@20.47.14 (150400-0):9)
        at ld.a(:com.google.android.gms.dynamite_measurementdynamite@204714097@20.47.14 (150400-0):3)
        at ef.run(:com.google.android.gms.dynamite_measurementdynamite@204714097@20.47.14 (150400-0):3)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at iy.run(:com.google.android.gms.dynamite_measurementdynamite@204714097@20.47.14 (150400-0):5)
W/System: Ignoring header X-Firebase-Locale because its value was null.
W/System: Ignoring header X-Firebase-Locale because its value was null.
D/FirebaseAuth: Notifying id token listeners about user ( 8Ie2YvHqsORGduxey21b7st8Fdb2 ).
    Notifying auth state listeners about user ( 8Ie2YvHqsORGduxey21b7st8Fdb2 ).
 
android
firebase
firebase-realtime-database
asked on Stack Overflow Jan 9, 2021 by Guilherme Ribeiro • edited Jan 9, 2021 by Guilherme Ribeiro

2 Answers

0

Can you add the code for the progressbar please?

Also you should be adding an OnCompleteListener to the setValue as well...this call is a Task so if you want to make sure it was a success, you should also attach a Listener to it.

answered on Stack Overflow Jan 9, 2021 by ktbffh ktbffh
0

After some time i finally managed to solve this.

Basically I had to download the google-services.json from the firebase page and replace with the json in my project.

I must have done something wrong when configuring the firebase for the first time.

answered on Stack Overflow Jan 10, 2021 by Guilherme Ribeiro

User contributions licensed under CC BY-SA 3.0