简体   繁体   中英

How to solve private boolean error in Android Studio

The code containing the error is:

public class HomeActivity extends AppCompatActivity {

    BottomNavigationView bottomNavigation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);

 bottomNavigation = (BottomNavigationView) findViewById(R.id.bottomNavigation);
        bottomNavigation.setOnNavigationItemReselectedListener(navigation);

    }

    private BottomNavigationView.OnNavigationItemReselectedListener navigation =
            new BottomNavigationView.OnNavigationItemReselectedListener() {
                @Override
                public boolean onNavigationItemReselected(@NonNull MenuItem item) {

                    switch (item.getItemId())
                    {
                        case R.id.home:
                            Toast.makeText(HomeActivity.this, "HOME", Toast.LENGTH_SHORT).show();
                            break;

                        case R.id.category:
                            Toast.makeText(HomeActivity.this, "CATEGORY", Toast.LENGTH_SHORT).show();
                            break;
                    }
                    return true;

                }
            };
}

It is possible to see the error in this screenshot .

The code shows a Toast with the text corresponding to the selected view.

You are using the OnNavigationItemReselectedListener listener. The method onNavigationItemReselected has to return void instead of boolean .

Maybe you are looking for BottomNavigationView.OnNavigationItemSelectedListener listener.

Check your category id in menus file first. Its containing or not

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM