Android Firebase实时数据库无法正常工作

时间:2017-01-20 05:42:50

标签: android firebase firebase-realtime-database firebase-authentication

我正在尝试将数据插入Firebase实时数据库。我的应用程序的逻辑是在用户注册时在数据库的username列下创建一个包含passwordusers的新字段。

注册时,该应用需要电子邮件,用户名和密码。我正在使用Firebase Auth执行此操作。当我运行应用程序时,我可以成功注册,但不会在数据库中创建值。

如果我错过了什么,请帮助。这是我的代码。

package dev.mintz.loginapp;

import android.app.ProgressDialog;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;

import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    public static String username, password;
    //defining view objects
    private EditText editTextEmail;
    private EditText editTextPassword;
    private EditText editTextUsername;
    private Button buttonSignup;

    private TextView textViewSignin;

    private ProgressDialog progressDialog;


    //defining firebaseauth object
    private FirebaseAuth firebaseAuth;

    private DatabaseReference databaseReference;

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

        //initializing firebase auth object
        firebaseAuth = FirebaseAuth.getInstance();

        //if getCurrentUser does not returns null
        if(firebaseAuth.getCurrentUser() != null){
            //that means user is already logged in
            //so close this activity
            finish();

            //and open profile activity
            startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
        }

        //getting the database reference
        databaseReference = FirebaseDatabase.getInstance().getReference();
        //initializing views
        editTextEmail = (EditText) findViewById(R.id.editTextEmail);
        editTextUsername = (EditText) findViewById(R.id.editTextUsername);
        editTextPassword = (EditText) findViewById(R.id.editTextPassword);
        textViewSignin = (TextView) findViewById(R.id.textViewSignin);

        buttonSignup = (Button) findViewById(R.id.buttonSignup);

        progressDialog = new ProgressDialog(this);

        //attaching listener to button
        buttonSignup.setOnClickListener(this);
        textViewSignin.setOnClickListener(this);
    }

    private void registerUser(){

        //getting email and password from edit texts
        String email = editTextEmail.getText().toString().trim();
        String username = editTextUsername.getText().toString().trim();
        String password  = editTextPassword.getText().toString().trim();

        //checking if email and passwords are empty
        if(TextUtils.isEmpty(email)){
            Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show();
            return;
        }

        if(TextUtils.isEmpty(password)){
            Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show();
            return;
        }

        //if the email and password are not empty
        //displaying a progress dialog

        progressDialog.setMessage("Registering Please Wait...");
        progressDialog.show();

        //creating a new user
        firebaseAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        //checking if success
                        if(task.isSuccessful()){
                            saveUserInformation();
                            Toast.makeText(MainActivity.this,"Successfully registered",Toast.LENGTH_LONG).show();
                            finish();
                            startActivity(new Intent(getApplicationContext(), ProfileActivity.class));
                        }else{
                            //display some message here
                            Toast.makeText(MainActivity.this,"Registration Error",Toast.LENGTH_LONG).show();
                        }
                        progressDialog.dismiss();
                    }
                });

    }

    private void saveUserInformation() {
        //Getting values from database

        //creating a userinformation object
        UserInformation userInformation = new UserInformation(username,password);
        databaseReference.child("users").setValue(userInformation);


    }

    @Override
    public void onClick(View view) {

        if(view == buttonSignup){
            registerUser();
        }

        if(view == textViewSignin){
            //open login activity when user taps on the already registered textview
            startActivity(new Intent(this, LoginActivity.class));
        }

    }
}

错误日志

01-19 04:10:13.442 31951-31951/? I/art: Not late-enabling -Xcheck:jni (already on)
01-19 04:10:13.443 31951-31951/? W/art: Unexpected CPU variant for X86 using defaults: x86
01-19 04:10:13.474 31951-31951/? W/System: ClassLoader referenced unknown path: /data/app/dev.mintz.loginapp-1/lib/x86
01-19 04:10:13.482 31951-31951/? I/InstantRun: Instant Run Runtime started. Android package is dev.mintz.loginapp, real application class is null.
01-19 04:10:21.400 31951-31951/dev.mintz.loginapp W/System: ClassLoader referenced unknown path: /data/app/dev.mintz.loginapp-1/lib/x86
01-19 04:10:21.511 31951-31984/dev.mintz.loginapp W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
01-19 04:10:21.519 31951-31984/dev.mintz.loginapp W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
01-19 04:10:21.552 31951-31951/dev.mintz.loginapp D/FirebaseAuth: Notifying listeners about user ( pv8XHzgO75MvPpTyEfelKGmv3mw2 ).
01-19 04:10:21.562 31951-31951/dev.mintz.loginapp D/FirebaseApp: com.google.firebase.crash.FirebaseCrash is not linked. Skipping initialization.
01-19 04:10:21.563 31951-31984/dev.mintz.loginapp W/System: ClassLoader referenced unknown path: /system/priv-app/PrebuiltGmsCore/lib/x86
01-19 04:10:21.575 31951-31984/dev.mintz.loginapp D/ApplicationLoaders: ignored Vulkan layer search path /system/priv-app/PrebuiltGmsCore/lib/x86:/system/fake-libs:/system/priv-app/PrebuiltGmsCore/PrebuiltGmsCore.apk!/lib/x86:/system/lib:/vendor/lib for namespace 0xa9c8f090
01-19 04:10:21.585 31951-31951/dev.mintz.loginapp I/FA: App measurement is starting up, version: 9683
01-19 04:10:21.585 31951-31951/dev.mintz.loginapp I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
01-19 04:10:21.585 31951-31951/dev.mintz.loginapp D/FA: Debug-level message logging enabled
01-19 04:10:21.585 31951-31951/dev.mintz.loginapp D/FA: AppMeasurement singleton hash: 107909644
01-19 04:10:21.592 31951-31951/dev.mintz.loginapp V/FA: Collection enabled
01-19 04:10:21.592 31951-31951/dev.mintz.loginapp V/FA: App package, google app id: dev.mintz.loginapp, 1:755290135710:android:03e30c324c6ca56a
01-19 04:10:21.592 31951-31951/dev.mintz.loginapp I/FA: To enable faster debug mode event logging run:
                                                          adb shell setprop firebase.analytics.debug-mode dev.mintz.loginapp
01-19 04:10:21.594 31951-31984/dev.mintz.loginapp W/System: ClassLoader referenced unknown path: 
01-19 04:10:21.594 31951-31984/dev.mintz.loginapp W/System: ClassLoader referenced unknown path: /system/priv-app/PrebuiltGmsCore/lib/x86
01-19 04:10:21.599 31951-31984/dev.mintz.loginapp D/ApplicationLoaders: ignored Vulkan layer search path /system/priv-app/PrebuiltGmsCore/lib/x86:/system/fake-libs:/system/priv-app/PrebuiltGmsCore/PrebuiltGmsCore.apk!/lib/x86:/system/lib:/vendor/lib for namespace 0xa9c8f0d0
01-19 04:10:21.606 31951-31951/dev.mintz.loginapp V/FA: Registered activity lifecycle callback
01-19 04:10:21.608 31951-31951/dev.mintz.loginapp I/FirebaseInitProvider: FirebaseApp initialization successful
01-19 04:10:21.613 31951-31988/dev.mintz.loginapp V/FA: Using measurement service
01-19 04:10:21.613 31951-31988/dev.mintz.loginapp V/FA: Connecting to remote service
01-19 04:10:21.693 31951-31951/dev.mintz.loginapp W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
01-19 04:10:21.696 31951-31951/dev.mintz.loginapp V/FA: onActivityCreated
01-19 04:10:21.822 31951-31951/dev.mintz.loginapp I/DynamiteModule: Considering local module com.google.android.gms.firebase_database:4 and remote module com.google.android.gms.firebase_database:4
01-19 04:10:21.822 31951-31951/dev.mintz.loginapp I/DynamiteModule: Selected remote version of com.google.android.gms.firebase_database, version >= 4
01-19 04:10:21.825 31951-31951/dev.mintz.loginapp W/System: ClassLoader referenced unknown path: /data/user/0/com.google.android.gms/app_chimera/m/00000002/n/x86
01-19 04:10:21.836 31951-31951/dev.mintz.loginapp D/FirebaseApp: Notifying auth state listeners.
01-19 04:10:21.836 31951-31951/dev.mintz.loginapp D/FirebaseApp: Notified 1 auth state listeners.
01-19 04:10:21.849 31951-31951/dev.mintz.loginapp V/FA: onActivityCreated
01-19 04:10:21.858 31951-31988/dev.mintz.loginapp V/FA: Activity resumed, time: 9906709
01-19 04:10:21.867 31951-31992/dev.mintz.loginapp D/NetworkSecurityConfig: No Network Security Config specified, using platform default
01-19 04:10:21.879 31951-31988/dev.mintz.loginapp D/FA: Connected to remote service
01-19 04:10:21.879 31951-31988/dev.mintz.loginapp V/FA: Processing queued up service tasks: 1
01-19 04:10:21.929 31951-31995/dev.mintz.loginapp I/OpenGLRenderer: Initialized EGL, version 1.4
01-19 04:10:21.929 31951-31995/dev.mintz.loginapp D/OpenGLRenderer: Swap behavior 1
01-19 04:10:21.965 31951-31995/dev.mintz.loginapp E/EGL_emulation: tid 31995: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
01-19 04:10:21.965 31951-31995/dev.mintz.loginapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa0ebc360, error=EGL_BAD_MATCH
01-19 04:10:26.891 31951-31988/dev.mintz.loginapp V/FA: Inactivity, disconnecting from the service
01-19 04:10:31.896 31951-31984/dev.mintz.loginapp W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
01-19 04:13:23.901 31951-31951/dev.mintz.loginapp D/FirebaseAuth: Notifying listeners about a sign-out event.
01-19 04:13:23.912 31951-31951/dev.mintz.loginapp D/FirebaseApp: Notifying auth state listeners.
01-19 04:13:23.912 31951-31951/dev.mintz.loginapp D/FirebaseApp: Notified 1 auth state listeners.
01-19 04:13:23.913 31951-2386/dev.mintz.loginapp V/FA: Activity paused, time: 10088764
01-19 04:13:23.989 31951-31951/dev.mintz.loginapp V/FA: onActivityCreated
01-19 04:13:23.998 31951-2386/dev.mintz.loginapp V/FA: Activity resumed, time: 10088850
01-19 04:13:24.145 31951-31995/dev.mintz.loginapp E/EGL_emulation: tid 31995: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
01-19 04:13:24.145 31951-31995/dev.mintz.loginapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x85b0a2c0, error=EGL_BAD_MATCH
01-19 04:13:24.231 31951-31995/dev.mintz.loginapp D/OpenGLRenderer: endAllActiveAnimators on 0x85b31280 (RippleDrawable) with handle 0x869484c0
01-19 04:15:27.159 31951-4277/dev.mintz.loginapp V/FA: Activity paused, time: 10212010
01-19 04:15:27.240 31951-31951/dev.mintz.loginapp V/FA: onActivityCreated

                                                        [ 01-19 04:15:27.245  1602: 1627 D/         ]
                                                        HostConnection::get() New Host Connection established 0x8cab5540, tid 1627
01-19 04:15:27.251 31951-4277/dev.mintz.loginapp V/FA: Activity resumed, time: 10212102
01-19 04:15:27.350 31951-31995/dev.mintz.loginapp E/EGL_emulation: tid 31995: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
01-19 04:15:27.350 31951-31995/dev.mintz.loginapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x83719a00, error=EGL_BAD_MATCH
01-19 04:15:27.555 31951-31951/dev.mintz.loginapp W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
01-19 04:15:41.216 31951-31951/dev.mintz.loginapp W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
01-19 04:15:44.184 31951-31951/dev.mintz.loginapp W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
01-19 04:15:49.822 31951-31984/dev.mintz.loginapp W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
01-19 04:15:49.823 31951-31984/dev.mintz.loginapp W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
01-19 04:15:49.867 31951-31995/dev.mintz.loginapp E/EGL_emulation: tid 31995: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
01-19 04:15:49.867 31951-31995/dev.mintz.loginapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa0eee300, error=EGL_BAD_MATCH
01-19 04:15:56.011 31951-31963/dev.mintz.loginapp D/FirebaseAuth: Notifying listeners about a sign-out event.
01-19 04:15:56.011 31951-31951/dev.mintz.loginapp D/FirebaseApp: Notifying auth state listeners.
01-19 04:15:56.012 31951-31951/dev.mintz.loginapp D/FirebaseApp: Notified 1 auth state listeners.
01-19 04:15:56.015 31951-31963/dev.mintz.loginapp D/FirebaseAuth: Notifying listeners about user ( LuXbE7Pbe1gVjQb7XqAOCTXZbFF3 ).
01-19 04:15:56.015 31951-31951/dev.mintz.loginapp D/FirebaseApp: Notifying auth state listeners.
01-19 04:15:56.016 31951-31951/dev.mintz.loginapp D/FirebaseApp: Notified 1 auth state listeners.
01-19 04:15:56.103 31951-4277/dev.mintz.loginapp V/FA: Activity paused, time: 10240955

                                                       [ 01-19 04:15:56.108  1602: 1627 D/         ]
                                                       HostConnection::get() New Host Connection established 0x8ee8d640, tid 1627
01-19 04:15:56.675 31951-31995/dev.mintz.loginapp E/EGL_emulation: tid 31995: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
01-19 04:15:56.675 31951-31995/dev.mintz.loginapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x83609220, error=EGL_BAD_MATCH
01-19 04:15:56.676 31951-31951/dev.mintz.loginapp V/FA: onActivityCreated
01-19 04:15:56.683 31951-4277/dev.mintz.loginapp V/FA: Activity resumed, time: 10241534
01-19 04:15:56.815 31951-31995/dev.mintz.loginapp E/EGL_emulation: tid 31995: eglSurfaceAttrib(1174): error 0x3009 (EGL_BAD_MATCH)
01-19 04:15:56.815 31951-31995/dev.mintz.loginapp W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x869254e0, error=EGL_BAD_MATCH
01-19 04:15:56.863 31951-31951/dev.mintz.loginapp W/IInputConnectionWrapper: finishComposingText on inactive InputConnection
01-19 04:15:59.921 31951-31984/dev.mintz.loginapp W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
01-19 04:16:57.336 31951-5610/dev.mintz.loginapp V/FA: Recording user engagement, ms: 3600055
01-19 04:16:57.342 31951-5610/dev.mintz.loginapp I/FA: Tag Manager is not found and thus will not be used
01-19 04:16:57.345 31951-5610/dev.mintz.loginapp D/FA: Logging event (FE): _e, Bundle[{_o=auto, _et=3600055}]
01-19 04:16:57.355 31951-5610/dev.mintz.loginapp V/FA: Using measurement service
01-19 04:16:57.355 31951-5610/dev.mintz.loginapp V/FA: Connecting to remote service
01-19 04:16:57.360 31951-5610/dev.mintz.loginapp D/FA: Connected to remote service
01-19 04:16:57.360 31951-5610/dev.mintz.loginapp V/FA: Processing queued up service tasks: 1
01-19 04:17:02.369 31951-5610/dev.mintz.loginapp V/FA: Inactivity, disconnecting from the service

2 个答案:

答案 0 :(得分:0)

检查用户名和密码字段是否为空或尝试此代码几乎相似。

 final String emailId = mEditTextEmail.getText().toString() ;
 String password = mEditTextPassword.getText().toString() ;
 firebaseRef.createUser(emailId, password, new Firebase.ValueResultHandler<Map<String,Object>>() {
         @Override
         public void onSuccess(Map<String, Object> stringObjectMap) {
                            User user = new User();
                            user.setUsername(emailId);
                            user.setPassword(password);
                            firebaseRef.child("Users").child(stringObjectMap.get("uid").toString()).setValue(user);
                 }

答案 1 :(得分:0)

你必须把firebaseAuth.createUserWithEmailAndPassword()...放在else中,因为在代码中它会检查电子邮件和密码是否为空并继续其余的....所以请仔细考虑这个方法..

If (TextUtils.isEmpty(email) || TextUtils.isEmpty(password) ){
 if(TextUtils.isEmpty(email)){
        Toast.makeText(this,"Please enter email",Toast.LENGTH_LONG).show();
        return;
    }

    if(TextUtils.isEmpty(password)){

        Toast.makeText(this,"Please enter password",Toast.LENGTH_LONG).show();
        return;
    }
}
else {
    progressDialog.setMessage("Registering Please Wait...");
       progressDialog.show();

    //creating a new user
    firebaseAuth.createUserWithEmailAndPassword(email, password).........

}