Now, I am studying the android developer training. In 'Adding Action Buttons' I got some problems.
I wrote the xml code like below, but in MainActivity.java
, action_search
and action_settings
got error and compiler said it cannot be resolved or not a field. And then, I read the recommendation to fix the code that "create field action_search
in type id
". I've already wrote the code "@+id/action_search"
and "@+id/action_settings"
. Why this error appeared please let me know!
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- Search, should appear as action button -->
<item android:id="@+id/action_search"
android:icon="@drawable/ic_action_search"
android:title="@string/action_search"
android:showAsAction="ifRoom" />
<!-- Settings, should always be in the overflow -->
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:showAsAction="never" />
</menu>
EDIT: Code as requested
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);
}
}