java - 为什么我从 Firebase 得到 NULL 结果

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

我创建了一个新的用户注册屏幕,用户注册后,注册将转到登录屏幕

注册用户时,我希望应用程序验证数据库中用户是否存在(如果用户存在)。将用户移动到主页之前的应用程序可以打印该用户的一些值,例如电子邮件、帐户类型、用户名和帐户状态。这是真的还是假的?

但是当我这样做时,我在 ANDROID STUDIO 控制台上得到 (NULL)

我的登录屏幕

public class LoginScreen extends AppCompatActivity {
    private FirebaseAuth mAuth;

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


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_screen);
        final ProgressBar simpleProgressBar = (ProgressBar) findViewById(R.id.loginProgressBar);
        simpleProgressBar.setVisibility(View.INVISIBLE);

         mAuth = FirebaseAuth.getInstance();

    /*    mauthlistener = new FirebaseAuth.AuthStateListener() {
            @Override
            public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
                if (firebaseAuth.getCurrentUser() != null){


                }
            }
        }*/
        final String TAG = "MyActivity";

        final Button gotosignupbtn = findViewById(R.id.gosignup);
        gotosignupbtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent gotosignup = new Intent(LoginScreen.this, SignupScreen.class);
                startActivity(gotosignup);
                finish();
            }
        });


        final EditText email = findViewById(R.id.emaillogin);
        final EditText password = findViewById(R.id.passwordlogin);

        Button loginbt = findViewById(R.id.loginbtn);

        loginbt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                simpleProgressBar.setVisibility(View.VISIBLE);
                final String Email = email.getText().toString();
                final String Password = password.getText().toString();

                mAuth.signInWithEmailAndPassword(Email , Password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                      //  FirebaseUser  userr = FirebaseAuth.getInstance().getCurrentUser();
                        FirebaseUser user = firebaseAuth.getCurrentUser();
                        final String  userKey = user.getUid();
                       if (task.isSuccessful()){
                           simpleProgressBar.setVisibility(View.INVISIBLE);

                           myRef.child("users").child(userKey).addValueEventListener(new ValueEventListener() {
                               @Override
                               public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                                   String userKey = dataSnapshot.child("\n" + "Email").getValue(String.class);
                                   Log.d(TAG, "Name: " + userKey);


/*
                                   userdtails activestatus = dataSnapshot.getValue(userdtails.class);
                                   System.out.println(activestatus);*/

                               }

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

                               }
                           });
                           Toast.makeText(LoginScreen.this, "Authentication ok.",
                                   Toast.LENGTH_SHORT).show();
                           FirebaseAuth.getInstance().signOut();


                       }else{
                           simpleProgressBar.setVisibility(View.INVISIBLE);
                           Toast.makeText(LoginScreen.this, "Error.",
                                   Toast.LENGTH_SHORT).show();

                       }
                    }
                });
            }
        });


    }

}

This is screen od firebase child have make

logcat结果

2020-04-23 01:23:56.974 16391-16391/? I/m.example.wafe: Not late-enabling -Xcheck:jni (already on)
2020-04-23 01:23:57.063 16391-16391/? W/m.example.wafe: Unexpected CPU variant for X86 using defaults: x86
2020-04-23 01:23:58.910 16391-16436/com.example.wafer W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
2020-04-23 01:23:59.055 16391-16391/com.example.wafer I/FirebaseInitProvider: FirebaseApp initialization successful
2020-04-23 01:23:59.057 16391-16443/com.example.wafer I/DynamiteModule: Considering local module com.google.android.gms.measurement.dynamite:12 and remote module com.google.android.gms.measurement.dynamite:0
2020-04-23 01:23:59.058 16391-16443/com.example.wafer I/DynamiteModule: Selected local version of com.google.android.gms.measurement.dynamite
2020-04-23 01:23:59.101 16391-16438/com.example.wafer W/DynamiteModule: Local module descriptor class for com.google.firebase.auth not found.
2020-04-23 01:23:59.113 16391-16391/com.example.wafer W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@bd36e17
2020-04-23 01:23:59.118 16391-16443/com.example.wafer W/m.example.wafe: Unsupported class loader
2020-04-23 01:23:59.179 16391-16443/com.example.wafer W/m.example.wafe: Skipping duplicate class check due to unsupported classloader
2020-04-23 01:23:59.230 16391-16438/com.example.wafer I/FirebaseAuth: [FirebaseAuth:] Preparing to create service connection to gms implementation
2020-04-23 01:23:59.282 16391-16391/com.example.wafer W/m.example.wafe: Accessing hidden method Landroid/graphics/drawable/Drawable;->getOpticalInsets()Landroid/graphics/Insets; (light greylist, linking)
2020-04-23 01:23:59.282 16391-16391/com.example.wafer W/m.example.wafe: Accessing hidden field Landroid/graphics/Insets;->left:I (light greylist, linking)
2020-04-23 01:23:59.282 16391-16391/com.example.wafer W/m.example.wafe: Accessing hidden field Landroid/graphics/Insets;->right:I (light greylist, linking)
2020-04-23 01:23:59.282 16391-16391/com.example.wafer W/m.example.wafe: Accessing hidden field Landroid/graphics/Insets;->top:I (light greylist, linking)
2020-04-23 01:23:59.283 16391-16391/com.example.wafer W/m.example.wafe: Accessing hidden field Landroid/graphics/Insets;->bottom:I (light greylist, linking)
2020-04-23 01:23:59.371 16391-16443/com.example.wafer V/FA: onActivityCreated
2020-04-23 01:23:59.456 16391-16391/com.example.wafer W/m.example.wafe: Accessing hidden method Landroid/view/View;->computeFitSystemWindows(Landroid/graphics/Rect;Landroid/graphics/Rect;)Z (light greylist, reflection)
2020-04-23 01:23:59.457 16391-16391/com.example.wafer W/m.example.wafe: Accessing hidden method Landroid/view/ViewGroup;->makeOptionalFitsSystemWindows()V (light greylist, reflection)
2020-04-23 01:23:59.505 16391-16406/com.example.wafer I/m.example.wafe: Background concurrent copying GC freed 8715(2MB) AllocSpace objects, 10(264KB) LOS objects, 49% free, 1885KB/3MB, paused 980us total 242.804ms
2020-04-23 01:23:59.642 16391-16447/com.example.wafer V/FA: App measurement collection enabled
2020-04-23 01:23:59.647 16391-16447/com.example.wafer V/FA: App measurement enabled for app package, google app id: com.example.wafer, 1:1010017815503:android:4af4d088a0980531d0e0ba
2020-04-23 01:23:59.655 16391-16447/com.example.wafer I/FA: App measurement initialized, version: 25001
2020-04-23 01:23:59.655 16391-16447/com.example.wafer I/FA: To enable debug logging run: adb shell setprop log.tag.FA VERBOSE
2020-04-23 01:23:59.655 16391-16447/com.example.wafer I/FA: To enable faster debug mode event logging run:
      adb shell setprop debug.firebase.analytics.app com.example.wafer
2020-04-23 01:23:59.655 16391-16447/com.example.wafer D/FA: Debug-level message logging enabled
2020-04-23 01:23:59.768 16391-16447/com.example.wafer V/FA: Connecting to remote service
2020-04-23 01:23:59.807 16391-16447/com.example.wafer V/FA: Connection attempt already in progress
2020-04-23 01:24:00.600 16391-16391/com.example.wafer D/OpenGLRenderer: HWUI GL Pipeline
2020-04-23 01:24:00.602 16391-16447/com.example.wafer V/FA: Activity resumed, time: 59697897
2020-04-23 01:24:00.633 16391-16447/com.example.wafer I/FA: Tag Manager is not found and thus will not be used
2020-04-23 01:24:00.706 16391-16448/com.example.wafer D/HostConnection: HostConnection::get() New Host Connection established 0xe46982b0, tid 16448
2020-04-23 01:24:00.707 16391-16448/com.example.wafer D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_2 
2020-04-23 01:24:00.728 16391-16448/com.example.wafer I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
2020-04-23 01:24:00.728 16391-16448/com.example.wafer I/ConfigStore: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasHDRDisplay retrieved: 0
2020-04-23 01:24:00.729 16391-16448/com.example.wafer I/OpenGLRenderer: Initialized EGL, version 1.4
2020-04-23 01:24:00.729 16391-16448/com.example.wafer D/OpenGLRenderer: Swap behavior 1
2020-04-23 01:24:00.729 16391-16448/com.example.wafer W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
2020-04-23 01:24:00.729 16391-16448/com.example.wafer D/OpenGLRenderer: Swap behavior 0
2020-04-23 01:24:00.754 16391-16448/com.example.wafer D/eglCodecCommon: setVertexArrayObject: set vao to 0 (0) 0 0
2020-04-23 01:24:00.754 16391-16448/com.example.wafer D/EGL_emulation: eglCreateContext: 0xe475c860: maj 2 min 0 rcv 2
2020-04-23 01:24:00.790 16391-16448/com.example.wafer D/EGL_emulation: eglMakeCurrent: 0xe475c860: ver 2 0 (tinfo 0xec2317c0)
2020-04-23 01:24:00.827 16391-16448/com.example.wafer D/HostConnection: createUnique: call
2020-04-23 01:24:00.827 16391-16448/com.example.wafer D/HostConnection: HostConnection::get() New Host Connection established 0xe4699b10, tid 16448
2020-04-23 01:24:00.834 16391-16391/com.example.wafer W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@f8b56da
2020-04-23 01:24:00.858 16391-16448/com.example.wafer D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_dma_v1 ANDROID_EMU_YUV420_888_to_NV21 ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_gles_max_version_2 
2020-04-23 01:24:00.858 16391-16448/com.example.wafer E/eglCodecCommon: GoldfishAddressSpaceHostMemoryAllocator: ioctl_ping failed for device_type=5, ret=-1
2020-04-23 01:24:00.940 16391-16443/com.example.wafer V/FA: onActivityCreated
2020-04-23 01:24:00.988 16391-16391/com.example.wafer W/m.example.wafe: Accessing hidden method Landroid/widget/TextView;->getTextDirectionHeuristic()Landroid/text/TextDirectionHeuristic; (light greylist, linking)
2020-04-23 01:24:01.028 16391-16447/com.example.wafer V/FA: Connection attempt already in progress
2020-04-23 01:24:01.029 16391-16447/com.example.wafer V/FA: Connection attempt already in progress
2020-04-23 01:24:01.033 16391-16447/com.example.wafer V/FA: Screen exposed for less than 1000 ms. Event not sent. time: 57
2020-04-23 01:24:01.049 16391-16447/com.example.wafer V/FA: Connection attempt already in progress
2020-04-23 01:24:01.049 16391-16447/com.example.wafer V/FA: Activity paused, time: 59697960
2020-04-23 01:24:01.189 16391-16447/com.example.wafer V/FA: Activity resumed, time: 59698486
2020-04-23 01:24:01.211 16391-16448/com.example.wafer D/EGL_emulation: eglMakeCurrent: 0xe475c860: ver 2 0 (tinfo 0xec2317c0)
2020-04-23 01:24:01.454 16391-16447/com.example.wafer V/FA: Connection attempt already in progress
2020-04-23 01:24:01.454 16391-16447/com.example.wafer V/FA: Connection attempt already in progress
2020-04-23 01:24:01.476 16391-16447/com.example.wafer D/FA: Connected to remote service
2020-04-23 01:24:01.502 16391-16447/com.example.wafer V/FA: Processing queued up service tasks: 7
2020-04-23 01:24:01.505 16391-16448/com.example.wafer D/EGL_emulation: eglMakeCurrent: 0xe475c860: ver 2 0 (tinfo 0xec2317c0)
2020-04-23 01:24:01.527 16391-16453/com.example.wafer D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2020-04-23 01:24:03.328 16391-16391/com.example.wafer I/AssistStructure: Flattened final assist data: 3128 bytes, containing 1 windows, 10 views
2020-04-23 01:24:06.825 16391-16447/com.example.wafer V/FA: Inactivity, disconnecting from the service
2020-04-23 01:24:10.645 16391-16391/com.example.wafer I/AssistStructure: Flattened final assist data: 3192 bytes, containing 1 windows, 10 views
2020-04-23 01:24:16.381 16391-16391/com.example.wafer I/BiChannelGoogleApi: [FirebaseAuth: ] getGoogleApiForMethod() returned Gms: com.google.firebase.auth.api.internal.zzaq@8ae9715
2020-04-23 01:24:17.676 16391-16414/com.example.wafer D/FirebaseAuth: Notifying id token listeners about user ( 5RulCyqAh7WexMzorDjbhvFMxtu2 ).
2020-04-23 01:24:17.682 16391-16414/com.example.wafer D/FirebaseAuth: Notifying auth state listeners about user ( 5RulCyqAh7WexMzorDjbhvFMxtu2 ).
2020-04-23 01:24:17.725 16391-16391/com.example.wafer D/FirebaseAuth: Notifying id token listeners about a sign-out event.
2020-04-23 01:24:17.725 16391-16391/com.example.wafer D/FirebaseAuth: Notifying auth state listeners about a sign-out event.
2020-04-23 01:24:17.851 16391-16448/com.example.wafer D/EGL_emulation: eglMakeCurrent: 0xe475c860: ver 2 0 (tinfo 0xec2317c0)
2020-04-23 01:24:17.998 16391-16391/com.example.wafer I/MyActivity: Name: null
2020-04-23 01:24:18.315 16391-16448/com.example.wafer D/EGL_emulation: eglMakeCurrent: 0xe475c860: ver 2 0 (tinfo 0xec2317c0)

最佳答案

如果您尝试提取数据,请执行以下操作:

......
myRef.child("users").child(userKey).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

//get the data of the user
String accountType = dataSnapshot.child("AcountType").getValue(String.class);
String companyName = dataSnapshot.child("CompanyName").getValue(String.class);
String Email = dataSnapshot.child("Email").getValue(String.class);
String phone = dataSnapshot.child("Phone").getValue(String.class);
String username = dataSnapshot.child("Username").getValue(String.class);
String activestatus = dataSnapshot.child("activestatus").getValue(String.class);
String email = dataSnapshot.child("email").getValue(String.class);
......
......
......

更新

这些:

FirebaseUser user = firebaseAuth.getCurrentUser();
final String  userKey = user.getUid();

必须在此处添加:

if (task.isSuccessful()){

//here
FirebaseUser user = firebaseAuth.getCurrentUser();
final String  userKey = user.getUid();
.....
....

关于java - 为什么我从 Firebase 得到 NULL 结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61376533/

相关文章:

ios - 在 Swift 中更新用户的 Firebase 密码

java - 二叉搜索树插入 - 根保持为空

java - 如何将字符转换为字符串

android - Appium:Android 资源 ID:在他们的 selenium 集成中转化为什么?

android - textview 不显示文本(一个 TextView 将另一个 TextView 推离屏幕很远)

android - 如何在确定的位置膨胀 View ?

java - 从单词中删除开头的标点符号

java - 为什么 HashMap 和 Hashtable 的 put 方法有区别?

javascript - Firebase 的异步/等待云功能,性能低下

android - 如何使用电子邮件身份验证允许对 Firebase 数据库进行读/写访问