在MainyActivity.java中设置搜索和设置代码时出错
我是Android Studio的新手。我通过developer.android.com学习编程。我正在添加操作栏,但现在我面临一个错误在MainyActivity.java中设置搜索和设置代码时出错,java,android,search,settings,Java,Android,Search,Settings,我是Android Studio的新手。我通过developer.android.com学习编程。我正在添加操作栏,但现在我面临一个错误 ` @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle presses on the action bar items switch (item.getItemId()) {
`
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
private void openSearch() {
Toast.makeText(this, "Search button pressed", Toast.LENGTH_SHORT).show();
}
return true;
case R.id.action_settings:
private void openSettings() {
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
}
return true;
default:
return super.onOptionsItemSelected(item);
}
}
这是我的代码,在
openSearch()
和openSettings()
之后,我在添加“;”时出错`但当我添加它时,它再次显示了预期的表达式。请尽快帮助我。提前谢谢 这是因为您正在开关盒内部创建一个方法
将方法拉出并在内部使用,如下所示:
private void openSearch() {
Toast.makeText(this, "Search button pressed", Toast.LENGTH_SHORT).show();
}
private void openSettings() {
startActivityForResult(new Intent(android.provider.Settings.ACTION_SETTINGS), 0);
}
@Override
public boolean onOptionsItemSelected(MenuItem 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);
}
}
乐于帮助:)快乐编码!