Reputation: 675
i have a menu that contains just one item.
Button exit;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
super.onCreateOptionsMenu(menu);
MenuInflater blowUp = getMenuInflater();
blowUp.inflate(R.menu.exitmenu, menu);
return true;
}
exit=(Button)findViewById(R.id.bexitMenuExit);
if i add listener to exit button , i got excpetion (null pointer), i am sure that there is no syntax error, the button exit is comming from this menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/bexitMenuExit"
android:title="Exit"
android:icon="@android:drawable/ic_menu_close_clear_cancel"
></item>
</menu>
what am i doing wrong?
Upvotes: 0
Views: 1643
Reputation: 1
for menu items, as stated by Sajmon, you can use onOptionsIemsSelected function. onClickListener is used by views while this function has been designed to be used specifically by menu items using switch case.
Upvotes: 0
Reputation: 33495
There is no need to initialize Button
and this doesn't make sence for me.
Just inflate your Menu
and just override onOptionsItemSelected method:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(getApplicationContext()).inflate(R.menu.exitmenu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getId()) {
case R.id.bexitMenuExit:
// work that will start when you click on this
...
}
}
Also, there is no need to use OnClickListener
, for this there is onOptionsItemSelected
method and you should use it.
Upvotes: 2