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?
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);