代码之家  ›  专栏  ›  技术社区  ›  ImTheNun

Firebase/抽屉布局-无故注销?

  •  0
  • ImTheNun  · 技术社区  · 6 年前

    我正在处理抽屉布局,当我单击其中一个项目以转到配置文件页面时,它会将我注销(我可以注意到这一点,因为我得到了UserAuth的NullPointer错误[尽管我在单击此之前已经过身份验证]

    注意:直到我尝试通过添加“initialize”方法等模块化代码时,我才注意到这一点。

    这是抽屉布局中项目的代码:

    private void setupDrawerLayout() {
            drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            navigationView = (NavigationView) findViewById(R.id.navigation_view);
            navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override public boolean onNavigationItemSelected(MenuItem menuItem) {
                    //If Clicked...then get item and go to associated page
                    switch(menuItem.getTitle().toString())
                    {
                        case "Profile":
                            Log.i("Profile", menuItem.getTitle().toString());
                            startActivity(new Intent(HomeActivity.this, ProfileActivity.class));
                        /*case "Share":
                            startActivity(new Intent(HomeActivity.this, ProfileActivity.class));
                        case "Settings":
                            startActivity(new Intent(HomeActivity.this, ProfileActivity.class)); */
                        case "Logout":
                            Log.i("Logout", menuItem.getTitle().toString());
                            FirebaseAuth.getInstance().signOut();
                            //startActivity(new Intent(HomeActivity.this, LoginActivity.class));
                    }
                    menuItem.setChecked(true);
                    drawerLayout.closeDrawers();
                    return true;
                    }
            });
        }
    

    出于某种原因,即使我正在单击“Profile”项,它也会转到该情况和“Logout”情况,如Log语句所示:

    I/Profile: Profile
    I/Logout: Profile
    

    然后这会导致错误:

    E/UncaughtException: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.netgalaxystudios.timeclock/com.netgalaxystudios.timeclock.Activities.ProfileActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
    
      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.google.firebase.auth.FirebaseUser.getUid()' on a null object reference
                             at com.netgalaxystudios.timeclock.Activities.ProfileActivity.onCreate(ProfileActivity.java:70)
    

    ProfileActivity。java(是我试图通过单击菜单中的项目来实现的活动)。它指的是这一行,但据我所知,错误实际上来自HomeActivity…:

     currentUserString = currentUser.getUid().toString();
    

    家庭活动。java:

    public class HomeActivity extends AppCompatActivity {
    
        private DrawerLayout drawerLayout;
        private NavigationView navigationView;
        ImageView menu, closeDrawer;
        RelativeLayout navHeader;
    
        //Profile Stuff
        DatabaseReference employeesRefPhoto, employeesRefFname, employeesRefLname;
        FirebaseDatabase database;
        String photoUrl;
        ImageView profilePhoto;
        FirebaseUser currentUser;
        String currentUserString;
        TextView nameTV, lnameTV;
        TextView businessesTV;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.drawer_layout);
    
            /////////////////////////
            //METHODS
            //initViews();
            //setupDrawerLayout();
            //footerOnClick();
            //////////////////////////
    
            menu = (ImageView) findViewById(R.id.menu);
            menu.setOnClickListener(new View.OnClickListener() {
                                        @Override
                                        public void onClick(final View view) {
                                                drawerLayout.openDrawer(Gravity.LEFT);
                                        }
                                    });
    
            currentUser = FirebaseAuth.getInstance().getCurrentUser();
            currentUserString = currentUser.getUid().toString();
    
            setupDrawerLayout();
    
            //https://stackoverflow.com/questions/37163579/null-pointer-exception-on-navigation-header-initialized-variables
            navHeader=(RelativeLayout) LayoutInflater.from(this).inflate(R.layout.drawer_header, null);
            navigationView.addHeaderView(navHeader); //This seems to add a SECOND "X" image
    
            nameTV = (TextView) navHeader.findViewById(R.id.employeeFname);
            lnameTV = (TextView) navHeader.findViewById(R.id.employeeLname);
    
            closeDrawer = (ImageView) navHeader.findViewById(R.id.closedrawer);
            profilePhoto = (ImageView) navHeader.findViewById(R.id.profilephoto);
            businessesTV = (TextView) findViewById(R.id.businesses);
    
             closeDrawer.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View view) {
                    drawerLayout.closeDrawer(Gravity.LEFT);
                }
            });
    
            ///////////////////////////////////////////////////////////////////////////
            //INFORMATION FOR MENU/////////////////////
            //PROFILE PHOTO
            //currentUser = FirebaseAuth.getInstance().getCurrentUser();
            //currentUserString = currentUser.getUid().toString();
    
            setupDrawerLayout();
    
            database = FirebaseDatabase.getInstance();
            employeesRefPhoto = database.getReference("Employees").child(currentUserString).child("photoDownloadUrl");
            employeesRefPhoto.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    Log.i("onDataChange","Inside");
    
                    try {
                        photoUrl = dataSnapshot.getValue().toString();
                        Log.i("photoUrl", photoUrl);
                        Glide.with(getApplicationContext()).load(photoUrl).into(profilePhoto);
                        profilePhoto.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    }
                    catch (NullPointerException e) {
                        Log.i("Null", e.toString());
                    }
    
                }
    
                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Log.d("Cancelled",databaseError.toString());
                }
            });
    
            //////////////////////////////////////////////
            //GET NAME//////////////////////////////////
            //FIRST NAME
            employeesRefFname = database.getReference("Employees").child(currentUserString).child("firstName");
            employeesRefFname.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    try {
                        Log.i("firstname", dataSnapshot.getValue().toString());
                        nameTV.setText(dataSnapshot.getValue().toString());
                    }
                    catch (Exception e) {  Log.i("FNull?", e.toString());     }
                }
    
                @Override
                public void onCancelled(DatabaseError databaseError) {   Log.d("Cancelled",databaseError.toString());     }
            });
    
            //LAST NAME
            employeesRefLname = database.getReference("Employees").child(currentUserString).child("lastName");
            employeesRefLname.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    try {
                        Log.i("lastname", dataSnapshot.getValue().toString());
    
                        lnameTV.setText(dataSnapshot.getValue().toString());
                    }
                    catch (Exception e) {  Log.i("LNull?", e.toString());     }
                }
    
                @Override
                public void onCancelled(DatabaseError databaseError) {   Log.d("Cancelled",databaseError.toString());     }
            });
    
            //footerOnClick();
            footerOnClick();
    
    
    
        } //END OF ONCREATE
    
        /////////////////////////////////////////////////////////////////////////////////
    
    
    
        //INITIALIZE VIEWS
        private void initViews(){
            navHeader=(RelativeLayout) LayoutInflater.from(this).inflate(R.layout.drawer_header, null);
    
            nameTV = (TextView) navHeader.findViewById(R.id.employeeFname);
            lnameTV = (TextView) navHeader.findViewById(R.id.employeeLname);
    
            menu = (ImageView) findViewById(R.id.menu);
            closeDrawer = (ImageView) navHeader.findViewById(R.id.closedrawer);
            profilePhoto = (ImageView) navHeader.findViewById(R.id.profilephoto);
            businessesTV = (TextView) findViewById(R.id.businesses);
        }
    
        //INTENTS FOR THE FOOTER OF THE SCREEN
        private void footerOnClick() {
    
            businessesTV.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(final View view) {
                    startActivity(new Intent(HomeActivity.this, BusinessesActivity.class));
                }
            });
    
            //EMPLOYEES
            //PAYROLL REPORT
        }
    
        //MENU / DRAWER
        //https://antonioleiva.com/navigation-view/
        private void setupDrawerLayout() {
            drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    
            navigationView = (NavigationView) findViewById(R.id.navigation_view);
            navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
                @Override public boolean onNavigationItemSelected(MenuItem menuItem) {
                    //If Clicked...then get item and go to associated page
                    switch(menuItem.getTitle().toString())
                    {
                        case "Profile":
                            Log.i("Profile", menuItem.getTitle().toString());
                            startActivity(new Intent(HomeActivity.this, ProfileActivity.class));
                        /*case "Share":
                            startActivity(new Intent(HomeActivity.this, ProfileActivity.class));
                        case "Settings":
                            startActivity(new Intent(HomeActivity.this, ProfileActivity.class)); */
                        case "Logout":
                            Log.i("Logout", menuItem.getTitle().toString());
                            FirebaseAuth.getInstance().signOut();
                            //startActivity(new Intent(HomeActivity.this, LoginActivity.class));
                    }
    
                    menuItem.setChecked(true);
                    drawerLayout.closeDrawers();
                    return true;
                }
            });
        }
    
        //??????????????????????????????????????
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            switch (item.getItemId()) {
                case android.R.id.home:
                    drawerLayout.openDrawer(GravityCompat.START);
                    return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    
    
    } //END OF CLASS
    
    1 回复  |  直到 6 年前
        1
  •  3
  •   Koushik Shom Choudhury    6 年前

    在…内 setupDrawerLayout() 在下面 onNavigationItemSelected() 您有一个switch语句。 在switch语句的每种情况下,使用 break 否则,可能会执行以下所有切换条件。由于最后一种情况是注销,因此每次执行switch语句时,用户都可能被注销。