【问题标题】:Error in setting Search and Settings code in MainyActivity.java在 MainyActivity.java 中设置搜索和设置代码时出错
【发布时间】:2015-10-04 08:56:50
【问题描述】:

我是 Android Studio 的新手。我正在通过 developer.android.com 学习编程。我正在添加操作栏,但现在我面临一个错误。

`

@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() 之后,添加“;”时出现错误,但是当我添加它时,它再次显示预期的表达式。请尽快帮助我。提前致谢!

【问题讨论】:

    标签: java android search settings


    【解决方案1】:

    这是因为您在 switch case 中创建了一个方法。

    把方法拉出来,在里面使用,像这样:

    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);
        }
    }
    

    【讨论】:

    • 乐于助人 :) 编码快乐!
    猜你喜欢
    • 2010-09-10
    • 1970-01-01
    • 1970-01-01
    • 2019-04-08
    • 2021-04-06
    • 1970-01-01
    • 2013-05-09
    • 1970-01-01
    相关资源
    最近更新 更多