私はアンドロイドチュートリアルを行っていますが、アクションボタンの追加部分では、openSearch() と openSettings() は定義されていません。そこで、同じクラスのプライベート ボイドとして作成しました。ただし、スイッチでは 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();
}