Android 从片段隐藏/显示导航元素

Android 从片段隐藏/显示导航元素,android,android-fragments,nullpointerexception,android-navigation,Android,Android Fragments,Nullpointerexception,Android Navigation,我想在用户未登录时从应用程序导航栏中隐藏“Profile”菜单项。当用户最终登录时,我想做两件事: 隐藏“登录”按钮 显示“配置文件”按钮 这应该是直截了当的,尽管我不确定如何访问这些我希望关闭的菜单项,而且当我尝试关闭时,会不断收到NullPointerException 我不能像我尝试过的那样,仅按id选择元素MenuItem profileBtn=view.findViewById(R.id.nav_profile); profileBtn.setVisible(true)当它抛出一个空点

我想在用户未登录时从应用程序导航栏中隐藏“Profile”菜单项。当用户最终登录时,我想做两件事:

  • 隐藏“登录”按钮
  • 显示“配置文件”按钮
  • 这应该是直截了当的,尽管我不确定如何访问这些我希望关闭的菜单项,而且当我尝试关闭时,会不断收到NullPointerException

    我不能像我尝试过的那样,仅按id选择元素
    MenuItem profileBtn=view.findViewById(R.id.nav_profile);
    profileBtn.setVisible(true)当它抛出一个空点异常时:

    java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.MenuItem.setVisible(boolean)' on a null object reference
    
    下面是loginScreen.java片段,我从中查看如何在底部的“onClick”方法中显示profile按钮和隐藏login按钮

    
    public class loginScreen extends Fragment implements View.OnClickListener{
    
        private loginScreenText loginScreenText;
        EditText email;
        EditText password;
        NavigationView navigationView;
        Button button;
        SimpleAccountManager accountManager;
        Context mContext;
    
        public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            setHasOptionsMenu(true);
    
            mContext = getActivity().getApplicationContext();
            accountManager = new SimpleAccountManager(mContext);
            button = (Button) inflater.inflate(R.layout.login_page, container, false).findViewById(R.id.loginBtn);
    
            loginScreenText =
            ViewModelProviders.of(this).get(loginScreenText.class);
            View root = inflater.inflate(R.layout.login_page, container, false);
            final TextView textView = root.findViewById(R.id.text_share);
            button = root.findViewById(R.id.loginBtn);
            email = root.findViewById(R.id.email);
            navigationView = root.findViewById(R.id.nav_view);
            password = root.findViewById(R.id.password);
            button.setOnClickListener(this);
    
            return root;
        }
    
        public void onClick(View view){
    
            if(view == button){
                User user;
                if(accountManager.getUserByEmail(email.getText().toString()) != null){
                    user = accountManager.getUserByEmail(email.getText().toString());
    
    
    /*              Attempt to reference the profile button which results in NullPointerException as shown 
                    above*/
    
                    MenuItem profileBtn= view.findViewById(R.id.nav_profile);
                    profileBtn.setVisible(true);
    
                    Fragment profile = new profileScreen(mContext);
                    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                    fragmentTransaction.replace(((ViewGroup)(getView().getParent())).getId(), profile);
                    fragmentTransaction.commit();
    
    
                    //kill current fragment
                    getFragmentManager().beginTransaction()
                            .remove(loginScreen.this).commit();
    
    
                    accountManager.login(user, password.getText().toString());
                }   else{
                    loginScreenText.setmText("No user found with email: " + email.getText().toString());
                }
    
            }
        }
    }
    
    
    以下是activity\u main\u drawer.xml中的菜单项

    
    

    提前感谢任何对如何解决此问题提出建议的人

    您需要先获取导航视图,然后获取其菜单,然后从中获取菜单项,而不是直接使用findViewById获取菜单项

    换言之:

    NavigationView navigationView = findViewById(R.id.navigation_view); //use the proper id
    Menu menu = navigationView.getMenu();
    MenuItem menuItem = menu.findItem(R.id.nav_profile);
    

    我在一个空对象引用上运行了虚拟方法“android.view.Menu com.google.android.material.navigation.NavigationView.getMenu()”后得到了这个结果?
    NavigationView navigationView = findViewById(R.id.navigation_view); //use the proper id
    Menu menu = navigationView.getMenu();
    MenuItem menuItem = menu.findItem(R.id.nav_profile);