隐藏/显示片段中的导航元素

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

  1. 隐藏“登录”按钮
  2. 显示“个人资料”按钮

这应该很简单,尽管我不确定如何访问要关闭的菜单项,并且在尝试访问时会不断收到NullPointerExceptions。

我通常不能像我尝试过MenuItem profileBtn= view.findViewById(R.id.nav_profile); profileBtn.setVisible(true);那样通过id选择元素,因为它会抛出 NullPointerException

java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.MenuItem.setVisible(boolean)' on a null object reference

这是 loginScreen.java 片段,在该片段中我希望显示配置文件按钮并在底部的“ onClick”方法中隐藏登录按钮。


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,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_main_drawer.xml

中的菜单项
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view"
    android:id="@+id/navMenu">

    <group android:checkableBehavior="single" >
        <item
            android:id="@+id/nav_home"
            android:icon="@drawable/ic_menu_camera"
            android:title="@string/menu_home" />
        <item
            android:id="@+id/nav_profile"
            android:icon="@drawable/ic_menu_gallery"
            android:title="Profile"
            android:visible="false"/>
        <item
            android:id="@+id/nav_polls"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="View Polls" />
        <item
            android:id="@+id/nav_patients"
            android:icon="@drawable/ic_menu_manage"
            android:title="View Patients" />
    </group>

    <item android:title="Log In!">
        <menu>
            <item
                android:id="@+id/nav_login"
                android:icon="@drawable/ic_menu_share"
                android:title="Login" />
            <item
                android:id="@+id/nav_login_sp"
                android:icon="@drawable/ic_menu_share"
                android:title="Login as SP" />
        </menu>
    </item>

</menu>

提前感谢任何人提供有关如何解决此问题的建议!

yuank888 回答:隐藏/显示片段中的导航元素

代替直接使用findViewById获取菜单项,您需要首先获取导航视图,然后获取其菜单,然后从那里获取菜单项。

换句话说:

NavigationView navigationView = findViewById(R.id.navigation_view); //use the proper id
Menu menu = navigationView.getMenu();
MenuItem menuItem = menu.findItem(R.id.nav_profile);
本文链接:https://www.f2er.com/3100272.html

大家都在问