我正在做 android 教程,在添加操作按钮部分, openSearch() 和 openSettings() 未定义。所以我把它们作为同一个类(class)的私有(private)空间。但在 switch 中,openSearch();显然是无法访问的。当我删除该案例时,下一个案例中的方法将无法访问。这是我的代码。
return super.onOptionsItemSelected(item);
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
private void openSettings() {
Toast.makeText(this, "Search button pressed", Toast.LENGTH_SHORT).show();
}
private void openSearch() {
Toast.makeText(this, "Search button pressed", Toast.LENGTH_SHORT).show();
}
最佳答案
你应该去掉方法顶部的return super.onOptionsItemSelected(item);
,否则开关将永远无法访问
关于java - Android教程切换第一种情况方法无法访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26087645/