0

I'm trying to use Bottom Navigation View to open another activity when clicked. For some reason, I'm getting an error on the last line, which should define the click function. I tested it on another project, and it worked perfectly.

Here's the end part of my Activity:

private BottomNavigationView.OnNavigationItemSelectedListener 
mOnNavigationItemSelectedListener
        = new BottomNavigationView.OnNavigationItemSelectedListener() {

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.navigation_home:
                Intent kampIntent = new Intent(SortimentActivity.this, CampaignActivity.class);
                startActivity(kampIntent);
                mTextMessage.setText(R.string.title_home);
                break;
            case R.id.navigation_dashboard:
                mTextMessage.setText(R.string.title_dashboard);
                break;
        }
        return false;


        BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
        navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
    }

I tried the exact same code in another project, and it worked. The error, which I get here, is

'Cannot resolve symbol' .setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

Any ideas on what went wrong here?

1 Answer 1

5

The error is telling you that it does not find mOnNavigationItemSelectedListener because you're calling it inside its definition

You have to move these two lines outside of the listener declaration. Put them on onCreate method, for example:

BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
2
  • Hmm, let me try to move it a bit
    – Rve
    Commented Sep 12, 2018 at 11:15
  • Wow, thanks it did work. Moved the two lines up a bit after the DB connection closing statement and it worked. Although I'm still probably having some errors in the code, since the application stopped among click event, but I still see a resolution here.
    – Rve
    Commented Sep 12, 2018 at 11:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.