我们可以根据用户级别设置android导航抽屉菜单吗?

时间:2019-05-25 12:24:26

标签: java android android-navigation-drawer

我正在使用导航抽屉活动创建一个应用程序。登录后,我的登录活动会将用户带到主要活动,其中包含导航抽屉。

我想根据JSON返回的用户角色来设置导航抽屉菜单,例如,role = 1将带您进入role-1的导航菜单,role = 2将为您显示的导航菜单角色2

我试图用if(role)条件设置onCreate,但它给我以下错误

“尝试调用虚拟方法'void android.support.design.widget.NavigationView.setNavigationItemSelectedListener)”

这是我尝试过的


 if(Integer.parseInt(String.valueOf(role))==1){

            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);
            View header = navigationView.getHeaderView(0);
            namaUser = (TextView) header.findViewById(R.id.NamaUser);
            emailUser = (TextView) header.findViewById(R.id.EmailUser);
            emailUser.setText(emailHolder);
            namaUser.setText(namaDepan +" "+namaBelakang);

            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);

            FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
            fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                }
            });

            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.addDrawerListener(toggle);
            toggle.syncState();

        }else{


                NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
                navigationView.setNavigationItemSelectedListener(this);
                View header = navigationView.getHeaderView(0);
                namaUser = (TextView) header.findViewById(R.id.NamaUser);
                emailUser = (TextView) header.findViewById(R.id.EmailUser);
                emailUser.setText(emailHolder);
                namaUser.setText(namaDepan +" "+namaBelakang);

                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                setSupportActionBar(toolbar);

                FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
                fab.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                                .setAction("Action", null).show();
                    }
                });
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.addDrawerListener(toggle);
            toggle.syncState();
        }


它给了我

“尝试调用虚拟方法'void android.support.design.widget.NavigationView.setNavigationItemSelectedListener)”

任何解决方案?

对不起,我的英语不好

0 个答案:

没有答案