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

问题描述 投票:0回答:1

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

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

这应该很简单,尽管我不确定如何才能关闭这些菜单项,并且在尝试关闭时,我一直在获取NullPointerExceptions。

我通常无法通过id选择元素,就像我尝试过MenuItem profileBtn= view.findViewById(R.id.nav_profile); profileBtn.setVisible(true);一样,因为它抛出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, 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_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>

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

android android-fragments android-navigation
1个回答
0
投票

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

换句话说:

NavigationView navigationView = findViewById(R.id.navigation_view);
Menu menu = navigationView.getMenu();
MenuItem menuItem = menu.findItem(R.id.nav_profile);
© www.soinside.com 2019 - 2024. All rights reserved.