记录时获取firebase用户详细信息,注销时忽略

时间:2021-11-12 21:53:45

I have a fragment that prints a message in textview depending on time of day and day of week. I want to print logged in first name but if user is logged out this first name should be ignored. App works fine when logged in but i get Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference when I log out.

我有一个片段,根据一天中的时间和星期几在textview中打印一条消息。我想打印登录的名字,但如果用户注销,则应忽略该名字。应用程序在登录时工作正常,但我在注销时尝试在空对象引用上调用虚拟方法'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()'。

String userID;
String currentUserId;
FirebaseAuth mAuth;
String mDayGreet;
String weekDay;
DatabaseReference dbReference = FirebaseDatabase.getInstance().getReference();
public static final String USER_ID_EXTRA_KEY = "EXTRA_KEY";

userID = getActivity().getIntent().getStringExtra(USER_ID_EXTRA_KEY);


    mAuth = FirebaseAuth.getInstance();
    FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
    if (firebaseUser!= null) {
        currentUserId = firebaseUser.getUid();
    }


    dbReference.child("AllUsers").child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("profile").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            User userDetail = dataSnapshot.getValue(User.class);

            Calendar mCalendar = Calendar.getInstance();
            SimpleDateFormat mFormatDate = new SimpleDateFormat("HH");
            String mDateString = mFormatDate.format(mCalendar.getTime());

            int time = Integer.parseInt(mDateString);
            TextView greeting = view.findViewById(R.id.greeting);
            int dayOfWeek = mCalendar.get(Calendar.DAY_OF_WEEK);
            TextView dayInfo = view.findViewById(R.id.info);

            if (Calendar.MONDAY == dayOfWeek) {
                if (time >= 0 && time < 18) {
                    weekDay = getString(R.string.monday);
                }
            }

            dayInfo.setText(weekDay);

            if (time >= 0 && time < 12)
                if (currentUserId == null) {
                    mDayGreet = getString(R.string.morning);
                } else {
                    mDayGreet = getString(R.string.morning) + " " + userDetail.getFirstName();
                }

            greeting.setText(mDayGreet);

1 个解决方案

#1


0  

You are using FirebaseAuth.getInstance().getCurrentUser().getUid() as the parameter of child method of database reference. But when the user is not logged in, the statement FirebaseAuth.getInstance().getCurrentUser() returns null and eventually you will get the NullPointerException when you try to call a method on it.

您正在使用FirebaseAuth.getInstance()。getCurrentUser()。getUid()作为数据库引用的子方法的参数。但是当用户未登录时,语句FirebaseAuth.getInstance()。getCurrentUser()将返回null,并且当您尝试在其上调用方法时,最终会得到NullPointerException。

One solution would be to save the user id in the shared preferences when the user is logged in, and if the user is not logged in next time, you can get the user id from shared preferences instead of FirebaseAuth.

一种解决方案是在用户登录时将用户ID保存在共享首选项中,如果用户下次未登录,则可以从共享首选项而不是FirebaseAuth获取用户ID。

#1


0  

You are using FirebaseAuth.getInstance().getCurrentUser().getUid() as the parameter of child method of database reference. But when the user is not logged in, the statement FirebaseAuth.getInstance().getCurrentUser() returns null and eventually you will get the NullPointerException when you try to call a method on it.

您正在使用FirebaseAuth.getInstance()。getCurrentUser()。getUid()作为数据库引用的子方法的参数。但是当用户未登录时,语句FirebaseAuth.getInstance()。getCurrentUser()将返回null,并且当您尝试在其上调用方法时,最终会得到NullPointerException。

One solution would be to save the user id in the shared preferences when the user is logged in, and if the user is not logged in next time, you can get the user id from shared preferences instead of FirebaseAuth.

一种解决方案是在用户登录时将用户ID保存在共享首选项中,如果用户下次未登录,则可以从共享首选项而不是FirebaseAuth获取用户ID。