如何解决Android Studio中的私有布尔错误

huangapple go评论78阅读模式
英文:

How to solve private boolean error in Android Studio

问题

错误所在的代码是:

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;

                }
            };
}

你可以在这个 截图链接 中看到错误。

这段代码显示一个包含所选视图对应文本的 Toast。

英文:

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.

答案1

得分: 1

你正在使用 OnNavigationItemReselectedListener 监听器。
方法 onNavigationItemReselected 必须返回 void,而不是 boolean

也许你正在寻找 BottomNavigationView.OnNavigationItemSelectedListener 监听器。

英文:

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

Maybe you are looking for BottomNavigationView.OnNavigationItemSelectedListener listener.

答案2

得分: 0

请先检查菜单文件中的类别ID,看它是否包含在内。

英文:

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

huangapple
  • 本文由 发表于 2020年9月7日 10:28:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63770635.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定