Cannot resolve method setHasOptionsMenu in android Activity
Asked Answered
L

2

11

I try to used setHasOptionsMenu(true); in my activity but i get an error. "cannot resolve method setHasOptionsMenu".

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.job_status_option_menu, menu);
    return super.onCreateOptionsMenu(menu);
}
Lantern answered 16/2, 2017 at 9:30 Comment(1)
Please show a complete example. In particular, you are missing the class which contains the methods you have shown.Tallyman
S
11

Try to remove setHasOptionMenu(true) from your onCreate() method, and change the onCreateOptionsMenu in this way:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.job_status_option_menu, menu);
    return true;
}
Submergible answered 16/2, 2017 at 9:35 Comment(0)
M
9

You should place this method call inside the oncreateview of fragment if you use fragments inside activity. Or else you don't have to use this invocation inside activity.

Michaelmas answered 16/2, 2017 at 9:31 Comment(4)
i am not using fragment, i am using activityLantern
Then why are you invoking this method?Michaelmas
@JihinRaju Activity does not have a method as setHasOptionsMenu(), this method belongs to Fragment class. You don't have to call that method. onCreateOptionsMenu is enough for Activities.Perlman
@AlvinVarghese I have try at onCreateView but still unresolvedNomi

© 2022 - 2024 — McMap. All rights reserved.