英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论