活动上的相同选项菜单,无效的方法声明; return 需要类型

Same Options Menu Over Activities, Invalid method declaration; return type required

我的项目中有多个活动使用相同的选项菜单。因此,我尝试对选项菜单功能使用单个 class,但是当我尝试调用这些功能时,出现此错误:

 Invalid method declaration; return type required

当我将函数分配给一个变量时,我得到了这个错误:

Error -- Expression expected

boolean optionCreate = super.onCreateOptionsMenu(menu);

和:

Error -- Cannot resolve symbol 'item'

boolean optionSelect = super.onOptionsItemSelected(item);

创建这些字段会导致使用选项菜单的 activity 无法加载。

我使用的代码是:

MainActivity.java


public class mainActivity extends OptionsMenu {

    ListView listView;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
        setSupportActionBar(myToolbar);
        getSupportActionBar().setTitle("Pick A Room");

        listView = (ListView) findViewById(R.id.listview);

        Intent intent = getIntent();
        String name = intent.getStringExtra("NAME");


        final ArrayList<String> arrayList = new ArrayList<>();

        arrayList.add("Big Fish");
        arrayList.add("Little Fish");
        arrayList.add("Cardboard Box");


        ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, arrayList);

        listView.setAdapter(arrayAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int index, long id) {
                openDialog();
            }
        });
    }

    public void openDialog(){
        TryAgainErrDialog errDialog = new TryAgainErrDialog();
        errDialog.show(getSupportFragmentManager(), "error dialog");
    }


    boolean optionCreate = super.onCreateOptionsMenu(menu);
    boolean optionSelect = super.onOptionsItemSelected(item);
}

OptionsMenu.java

public class OptionsMenu extends AppCompatActivity {

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.appbar, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.action_back:

                return true;
            case R.id.action_songQueue:

                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

}

不需要调用那些方法。它会被系统自动调用。从您的代码中删除它们。

//boolean optionCreate = super.onCreateOptionsMenu(menu);
//boolean optionSelect = super.onOptionsItemSelected(item);