java - Android options menu does not work? -
i have single activity in project. created options menu 3 items. don't know why not work me. every time select item item id 0.
res/menu/m1.xml:
<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/item1" android:title="one"> </item> <item android:id="@+id/item2" android:title="two"> </item> <item android:id="@+id/item3" android:title="three"> </item> </menu>
activity:
public class mainactivity extends activity { @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); } @override public boolean oncreateoptionsmenu(menu menu) { menu.add("1"); menu.add("2"); menu.add("3"); return super.oncreateoptionsmenu(menu); } @override public boolean onoptionsitemselected(menuitem item) { switch(item.getitemid()){ case r.id.item01: toast.maketext(this, "1",toast.length_short); break; case r.id.item02: toast.maketext(this, "2",toast.length_short); break; case r.id.item03: toast.maketext(this, "3",toast.length_short); break; default: toast.maketext(this, "-----",toast.length_short); } log.v("demo", "item id: "+item.getitemid()); return super.onoptionsitemselected(item); } }
when select item 0 in log. why?
replace oncreateoptionsmenu() method below code
@override public boolean oncreateoptionsmenu(menu menu) { getmenuinflater().inflate(r.menu.m1, menu); return true; }
Comments
Post a Comment