Search code examples
androidandroid-activityandroid-optionsmenu

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


I have multiple activities in my project which use the same options menu. Because of this I'm trying to use a single class for the option menu fuctions, however when I try call the functions I get this error:

 Invalid method declaration; return type required

When I assign the function to a variable I get this error from:

Error -- Expression expected

boolean optionCreate = super.onCreateOptionsMenu(menu);

and:

Error -- Cannot resolve symbol 'item'

boolean optionSelect = super.onOptionsItemSelected(item);

Creating these fields causes the activity that is using the options menu to not load.

The code I'm using is:

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

}


Solution

  • No need to call those method. It will invoke automatically by the system. Remove those from your code.

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